[英]mysql and php %like% query. Works in phpmyadmin but different results php
[英]Fetching multiple rows using mysql LIKE query. wrks in mysql but not in php
這是我的mysql查詢:
select studentID from GameMaster where hobbies like '%baseball%' and hobbies like '%basketball%';
愛好字段Type- LongText。
這將在mysql中獲取兩個值。 S1001,S1002
當我在PHP代碼中使用此代碼時,僅獲得第一行。
這是我的PHP代碼
$sSQL="select studentID from GameMaster where hobbies like '%baseball%' and hobbies like '%basketball%'";
$result = mysqli_query($dbc,$sSQL);
$numberOfRows = mysqli_num_rows($result);
if ($numberOfRows>0) {
for ($i = 0; $i < $numberOfRows; $i++) {
$row = mysqli_fetch_array($result);
$csv_output .= $row[$i].",";
}
$csv_output .= "\n";
}
print $csv_output;
$ csv_output的值為S1001,
如您所見,第二個是空的。 循環錯了嗎? 我正在將此值導出到應用發明家。
您不應遍歷$ row,它始終處於第一位! 而不是$row[$i]
應該是$row[0]
或$row['studentID']
if ($numberOfRows>0) {
for ($i = 0; $i < $numberOfRows; $i++) {
$row = mysqli_fetch_array($result);
$csv_output .= $row['studentID'].",";
}
$csv_output .= "\n";
}
除了for循環,您還可以使用while循環:
if ($numberOfRows>0) {
while($row = mysqli_fetch_array($result)) {
$csv_output .= $row['studentID'].",";
}
$csv_output .= "\n";
}
嘗試這個 :
$sSQL="select studentID from GameMaster where hobbies like '%baseball%' or hobbies like '%basketball%'";
$result = mysqli_query($dbc,$sSQL);
$numberOfRows = mysqli_num_rows($result);
if ($numberOfRows>0)
{
$row = mysqli_fetch_array($result);
for ($i = 0; $i < $numberOfRows; $i++)
{
$csv_output .= $row[$i].",";
}
$csv_output .= "\n";
}
print $csv_output;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.