简体   繁体   English

mysqli_query仅在有WHERE语句时返回结果

[英]mysqli_query only returning results when there's a WHERE statement

MySQL query is only showing information if I have a condition in the SQL statement. 如果我在SQL语句中有条件,MySQL查询仅显示信息。

I've successfully used the SQL statement in phpmyadmin and it works great. 我已成功使用phpmyadmin中的SQL语句,效果很好。 I've changed the table name in the PHP code and it functions properly, the "people" table is the only one that causes a problem. 我已经在PHP代码中更改了表名,并且它正常运行,“people”表是唯一导致问题的表。

<?php
include 'dbconnectLocal.php';

$sql = "SELECT * FROM people WHERE nameFirst = 'Karen'";

$billings = array();

$billingResults = mysqli_query($connL, $sql);

while($row = mysqli_fetch_assoc($billingResults)){
    $billings[] = $row;
}

mysqli_close($connL);

$jsonOutput = json_encode($billings);

print("<pre>".json_encode($billings, JSON_PRETTY_PRINT)."</pre>");
?>

The above code produces the desired result, it gives me a JSON result of everyone whos name is Karen. 上面的代码产生了所需的结果,它给了我一个JSON结果,每个名字都是Karen的人。 But if I were to change it to $sql = "SELECT * FROM people" I get a blank screen. 但如果我要将其更改为$ sql =“SELECT * FROM people”,我会得到一个空白屏幕。

Json have 4 mb limitation. Json有4 MB的限制。 You can make a var_dump of $billings to check what you get from db 您可以使用$ billings的var_dump来检查从db获得的内容

After much digging around and frustration, it turned out to be a json_encode error. 经过多次挖掘和挫折之后,结果证明是json_encode错误。 Specifically a UTF8 problem. 特别是UTF8问题。 Found this solution on php.net. 在php.net上找到了这个解决方案。 I ran the array through this function before encoding it and it worked perfect. 我在编码之前通过这个函数运行数组,它运行得很完美。

function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = utf8ize($v);
}
} else if (is_string ($d)) {
return utf8_encode($d);
}
return $d;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM