[英]Convert MySQL query into JSON object using PHP
I am new to programming and have a question about converting a MYSQL query with a Join into a JSON object using PHP. 我是编程新手,并且对使用PHP将带有Join的MYSQL查询转换为JSON对象有疑问。 When running the statement through phpMyAdmin I get results.
通过phpMyAdmin运行该语句时,得到结果。 However, when attempting the convert it into a JSON object I am getting a blank screen.
但是,当尝试将其转换为JSON对象时,出现黑屏。 Any help is greatly appreciated!
任何帮助是极大的赞赏! Here is my code:
这是我的代码:
$myquery = "SELECT track_ticseverity.date, track_ticseverity.ticnum, track_ticseverity.user_id, track_fatigue.date, track_fatigue.fatiguenum, track_fatigue.user_id
FROM track_ticseverity
INNER JOIN track_fatigue
ON track_ticseverity.date=track_fatigue.date
WHERE track_ticseverity.user_id=1
AND track_fatigue.user_id=1;"
$query = mysqli_query($conn, $myquery);
if ( ! $query ) {
echo mysqli_error();
die;
}
$data = array();
for ($x = 0; $x < mysqli_num_rows($query); $x++) {
$data[] = mysqli_fetch_assoc($query);
}
echo json_encode($data);
mysqli_close($server);
Did you connect to your database? 您连接到数据库了吗? (mysqli_connect)
(mysqli_connect)
It's not in your code example, but maybe you did anyway and didn't copy it. 它不在您的代码示例中,但是也许您仍然这样做并且没有复制它。
If you did, to which variable did you assign the connection? 如果已完成,则将连接分配给哪个变量? Once you're using $conn in mysqli_query and once youre using $server in mysqli_close.
一旦您在mysqli_query中使用$ conn,一次在mysqli_close中使用$ server。
Maybe this is helping already even I think PHP should show errors in this case? 也许这已经对我有所帮助,即使我认为PHP在这种情况下也应该显示错误?
Another Tipp 另一个提示
You can easily write the following: 您可以轻松地编写以下内容:
while($datarow = mysqli_fetch_assoc($query)) {
$data[] = $datarow;
}
Like this, you can save the for-loop. 这样,您可以保存for循环。
I just figured it out. 我只是想通了。 I ended my Query with ;" instead of ";
我用;“而不是”;结束查询 Thanks for your replies!
多谢您的回覆! Sorry I did not catch that before posting!
抱歉,发布之前我没有注意到!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.