mysql查询数组计数器

时间:2016-09-30 03:50:50

标签: php mysql arrays

如果我的术语有误,请道歉。

我在php中有一个for循环操作mysql查询...

for ($i = 0; $i <count($user_id_pc); $i++) 
{
$query2 = " SELECT job_title, job_info FROM job_description WHERE  postcode_ss = '$user_id_pc[$i]'";

$job_data = mysqli_query($dbc, $query2);

$job_results = array();

    while ($row = mysqli_fetch_array($job_data))
        {
            array_push($job_results, $row);
        }

}

插入...

时给出的结果

print_r ($job_results);

  

在屏幕上 - &gt;阵列()

如果我将查询从$user_id_pc[$i]更改为$user_id_pc[14],我会收到一组结果。

如果我在查询之后和for循环中包含此代码

echo $i;
echo $user_id_pc[$i] . "<br>";

我收到计数器$i所在的数字,然后是数组内该数据位置的数据。

我不确定为什么数组$job_results在使用计数器$i的查询中是空的,但如果我手动输入数字则不行?

这是一个我需要逃脱的特殊角色吗?

完整代码

        <?php
print_r ($user_id_pc);

  //Select all columns to see if user has a profile
     $query = "SELECT * FROM user_profile WHERE user_id = '" . $_SESSION['user_id'] . "'";

      //If the user has an empty profile direct them to the home page

  $data = mysqli_query($dbc, $query);

  if (mysqli_num_rows($data) == 0) 
    {
        echo '<br><div class="alert alert-warning" role="alert"><h3>Your appear not to be logged on please visit the<a href="index.php"> home page</a> to log on or register. <em>Thank you.</em></h3></div>';
    }
//Select data from user and asign them to variables
    else
    {
          $data = mysqli_query($dbc, $query);
      if (mysqli_num_rows($data) == 1) 
      {
          $row = mysqli_fetch_array($data);
          $cw_job_name = $row['job_description'];
          $cw_rate = $row['hourly_rate'];
          $job_mileage = $row['mileage'];
          $job_postcode = $row['postcode'];
          $response_id = $row['user_profile_id'];
      }
    }


    for ($i = 0; $i <count($user_id_pc); $i++) 
        {
            $query2 = " SELECT job_title, job_info FROM job_description WHERE  postcode_ss = '{$user_id_pc[$i]}'";                                        
    $job_data = mysqli_query($dbc, $query2);
    $job_results = array();
        while ($row = mysqli_fetch_array($job_data))
            {
                array_push($job_results, $row);
            }

    echo $i;
?>
<br>
<?php

}
print ($query2);
print $user_id_pc[$i];
         ?>

3 个答案:

答案 0 :(得分:0)

这主要是语法错误,正确的语法应该是:

$query2 = " SELECT job_title, job_info FROM job_description WHERE  postcode_ss = '{$user_id_pc[$i]}'";

请注意,这是正确的语法,但仍然是错误的!出于两个原因,第一个原因是它几乎总是更好(更快,更高效,占用更少的资源)来进行连接或子查询或简单的IN(数组)类型查询,而不是多次循环和查询。

第二个问题是以这种方式传递参数会使您容易受到SQL注入攻击。你应该使用准备好的陈述。

正确方法

if(count($user_id_pc)) {
    $stmt = mysqli_stmt_prepare(" SELECT job_title, job_info FROM job_description WHERE  postcode_ss = ?");
    mysqli_stmt_bind_param($stmt, "s", "'" . implode("','",$user_id_pc) . "'");
    mysqli_stmt_execute($stmt);
}

请注意,for循环已被简单的if

替换

答案 1 :(得分:0)

您必须检查查询变量,而不是:

$query2 = " SELECT job_title, job_info FROM job_description WHERE  postcode_ss = '$user_id_pc[$i]'"
你尝试过这个:

$query2 = " SELECT job_title, job_info FROM job_description WHERE  postcode_ss = '" . $user_id_pc[$i] . "' ";

另外,尝试不同的东西:

while ($row = mysqli_fetch_array($job_data))
    {
        $job_results[] = array("job_title" => $row["job_title"], "job_info" => $row["job_info");
    }

然后尝试打印值。

答案 2 :(得分:0)

很抱歉,但我喜欢 foreach() ,所以您的工作代码是:

<?php

 // To store the result
 $job_results = [];

 foreach($user_id_pc as $id ){
   // selecting matching rows   
   $query2 ="SELECT job_title, job_info FROM job_description WHERE postcode_ss = '".$id."'";
   $job_data = mysqli_query($dbc, $query2);
   // checking if query fetch any result
   if(mysqli_num_rows($job_data)){
      // fetching the result
      while ($row = mysqli_fetch_array($job_data)){
        // storing resulting row
        $job_results[] = $row;
     }
   }
}
// to print the result
var_dump($job_results);