我有一个问题,希望你能帮助我......
我正在使用WHERE / IN执行一个大型SELECT语句,并为所有搜索条件传入$数组'使用。
快速示例..(虽然最后$ array更大)
$harNumArray = (0100001943,0100001944,0100002392,0100007414,0100012110,0100015761,0100015835);
$harNumArray2 = implode(',', $harNumArray);
$results = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements WHERE har_id IN ($harNumArray2)");
//**outputting the matches values/data from the search (select)**
while ($row = mysqli_fetch_assoc($results)) {
//echo "HAR_ID: ". $row["har_id"]. "\tGUAR_NUM: " . $row["guar_num"] . "\r\n<BR>";
echo $row["har_id"]. "\t" . $row["guar_num"] . "\r\n<BR>";
fwrite($fh, $row["har_id"] . "\t" . $row["guar_num"] . "\r\n");
}
这很好/很好(*感谢msturdy提示)......&#39;当&#39;有一个匹配..如果没有匹配..没有返回。
但是..例如,如果在数据库中找不到$ harNumArray(索引0)中的第一个值... $ row = mysqli_fetch_assoc中没有返回任何内容($ results循环...
仅查找数字..以及从中提取数据的匹配/关联数据..
我希望它仍然像这样:
或者这样可以接受(但是上面的布局会更好):
如果在WHERE / IN SELECT中找不到$ harNumArray数组中的第一个数字..它不会返回任何内容(这是有意义的)..
但是......我需要&#39;考虑不匹配..并插入一个空白(或其他东西)..以保持最终“#”列表的间距/顺序。 (文本文件)我该怎么做呢?
谢谢!
修改::
这是当前的代码状态:(仍然不返回匹配项)
@Cal
//stack overflow approach:
//format array data
$harNumArray2 = "'" . implode("','", $harNumArray) . "'"; //single quotes
//$harNumArray2 = implode(',', $harNumArray); //no quotes
//$harNumArray2 = '"' . implode('","', $harNumArray) . '"'; //double quotes
//print_r("ARRAY CHECK: " . $harNumArray2);
$results = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements WHERE har_id IN ($harNumArray2) ORDER BY har_id ASC") or die(mysql_error());
//$results = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements WHERE har_id IN (" . $harNumArray2 . ") ORDER BY har_id ASC") or die(mysql_error());
echo("<BR>");
print_r("SELECT har_id, guar_num FROM placements WHERE har_id IN ($harNumArray2)");
echo("<BR>");
$rows = array();
while($row = mysqli_fetch_assoc($results)) {
$rows[$row['har_id']] = $row;
}
foreach ($harNumArray as $id){
if (isset($rows[$id])){
//... do something with $rows[$id]
print_r($rows[$id] . "<BR>");
}else{
//... no match for $id
print_r("....no match....");
echo("<BR>");
}
}
仍然没有返回匹配...无论是单引号,没有引号还是双引号..
难倒?
感谢
更新II:
@Cal -
好的,我运行了你提供的代码..
这是我的输出:
数组([har_id] =&gt; 000100007537 [guar_num] =&gt; 0000676798)数组([har_id] =&gt; 000100007538 [guar_num] =&gt; 0000676798)数组([har_id] =&gt; 000100007539 [guar_num] = &gt; 0000676798)数组([har_id] =&gt; 000100007768 [guar_num] =&gt; 0000675266)数组([har_id] =&gt; 000100007769 [guar_num] =&gt; 0000675266)
DB = varchar(12)中的HAR_ID
DB = varchar(12)中的GUAR_NUM
如果我在#results查询中留下引号。我得到数据返回..(但无论如何......没有找到匹配项)
答案 0 :(得分:0)
执行查询:
$harNumArray = array(
'0100001943',
'0100001944',
'0100002392',
'0100007414',
'0100012110',
);
$harNumArray2 = "'".implode("','", $harNumArray)."'";
$results = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements WHERE har_id IN ($harNumArray2)");
然后构建结果地图:
$rows = array();
while ($row = mysqli_fetch_assoc($results)) {
$rows[$row['har_id']] = $row;
}
然后你可以循环检查匹配的ID:
foreach ($harNumArray as $id){
if (isset($rows[$id])){
... do something with $rows[$id]
}else{
... no match for $id
}
}
更新1:
听起来你的查询没有返回任何内容。运行以下代码:
$results = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements LIMIT 5");
while ($row = mysqli_fetch_assoc($results)) print_r($row);
然后用输出更新你的问题。听起来您的数据库并不包含您的想法。