我从mysql获取数据并将其转换为JSON,但结果收到一个空数组:[]
我想选择category = given variable:
的所有数据$flu = $_POST['searchCode'];
$query = mysql_query("SELECT * From catalog_Master WHERE category='%$flu%'");
$rows = array();
while($row = mysql_fetch_assoc($query)) {
$rows[] = $row;
}
echo json_encode($rows);
答案 0 :(得分:5)
要让%
在MySQL中工作,您需要LIKE
:
$query=mysql_query("SELECT * From catalog_Master WHERE category LIKE '%$flu%'");
您也可以匹配开头或结尾:
$query=mysql_query("SELECT * From catalog_Master WHERE category LIKE '$flu%'");
$query=mysql_query("SELECT * From catalog_Master WHERE category LIKE '%$flu'");
如果您想要完全匹配,请丢失%
和LIKE
:
$query=mysql_query("SELECT * From catalog_Master WHERE category='$flu'");
此外,您应该验证$_POST
个变量(至少使用mysql_real_escape_string
,更好地使用mysqli或PDO)。
答案 1 :(得分:0)
试试这个
$query =mysql_query("SELECT * From catalog_Master WHERE category like '".$flu."%'");
答案 2 :(得分:0)
试试这个:
$flu = $_POST['searchCode'];
$query = mysql_query("SELECT * From catalog_Master WHERE category LIKE '%".$flu.%"'");
$rows = array();
while($row = mysql_fetch_assoc($query)) {
$rows[] = $row;
}
echo json_encode($rows);