0

我从 mysql 获取数据并将其转换为 JSON,但结果收到一个空数组:[]

我想选择类别=给定变量的所有数据:

$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);
4

3 回答 3

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,最好使用mysqliPDO)。

于 2013-03-26T11:03:53.363 回答
0

尝试这个

$query =mysql_query("SELECT * From catalog_Master WHERE  category like '".$flu."%'");
于 2013-03-26T11:06:20.900 回答
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);
于 2013-03-26T11:23:51.733 回答