从mysql获取数据显示空字符串

时间:2013-03-26 11:01:39

标签: php mysql

我从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);

3 个答案:

答案 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,更好地使用mysqliPDO)。

答案 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);