简体   繁体   English

使用数组元素运行SQL查询

[英]running a sql query with array elements

what I am trying to do is extract elements of an array and running an sql query with the array element as the condition. 我想做的是提取数组的元素并以该数组元素为条件运行sql查询。 The problem what I am facing is that the query does not return anything. 我面临的问题是查询不返回任何内容。 The code is given below 代码如下

//extracting the array elements
      foreach ($t as $value) {
      extract($value);

     }
 $sql = "SELECT * FROM daily_log where employee_log_id='$employee_log_id' AND log_date='$value'</br>";
            $result = mysql_query($sql);
            $row = mysql_fetch_array($result);
        echo $row['in_time'];
        echo $row['out_time'];

The echo $row['in_time'] and echo $row['out_time']; echo $ row ['in_time'] and echo $ row ['out_time']; does not show anything. 什么也没显示。

Can anybody help me to figure out what the problem is Thanks in advance. 有人可以帮我弄清楚问题出在哪里吗,谢谢。

The problem is your sql query code is outside the foreach loop. 问题是您的sql查询代码在foreach循环之外。 The $value variable doesn't exist in that scope. $value变量在该范围中不存在。
Try this: 尝试这个:

foreach ($t as $value) {
    extract($value);
    $sql = "SELECT * FROM daily_log where employee_log_id='$employee_log_id' AND log_date='$value'</br>";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    echo $row['in_time'];
    echo $row['out_time'];
}

EDIT: 编辑:
But I can't understand what you are trying to do. 但是我不明白你要做什么。 I think you either need the extract() function or the foreach loop. 我认为您要么需要extract()函数,要么需要foreach循环。 Also, why is there a </br> html tag in your sql query string? 另外,为什么您的sql查询字符串中有</br> html标记?
Do you want something like this? 你想要这样的东西吗?

foreach ($t as $key -> $value) {
    $sql = "SELECT * FROM daily_log where employee_log_id='$key' AND log_date='$value'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    echo $row['in_time'];
    echo $row['out_time'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM