This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
我从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);

最佳答案

为了使%在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)。

关于php - 从mysql获取数据显示空字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15635398/

10-09 16:02
查看更多