![](/img/trans.png)
[英]php: looping thru results from mysql query to increment counter (associative array)
[英]mysql query array counter
抱歉,如果我有錯誤的術語。
我在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);
在屏幕上-> Array()
例如,如果我將查詢從$user_id_pc[$i]
更改$user_id_pc[14]
, $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];
?>
這主要是語法錯誤,正確的語法應為:
$query2 = " SELECT job_title, job_info FROM job_description WHERE postcode_ss = '{$user_id_pc[$i]}'";
請注意,這是正確的語法,但仍然是錯誤的! 出於兩個原因,第一個原因是執行聯接或子查詢或簡單的IN(array)類型查詢比遍歷和查詢多次幾乎總是更好(更快,更高效,占用更少的資源)。
第二個問題是,以這種方式傳遞參數使您容易受到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代替
您必須檢查查詢變量,而不是:
$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");
}
然后嘗試打印值。
抱歉,但是我喜歡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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.