我设置了我的本地主机,我已经准备好了所有相关文件。我运行php运行sql查询,它返回NULL值。但它不应该。我通过浏览器访问链接目录。
有人能指出我的返回方向,为什么它会返回NULL?我运行SQL查询,并在我的phpmyAdmin
上正常工作<?php
header('Content-Type: application/json');
define('DB_HOST', '127.0.0.1');
define('DB_USERNAME', 'myusername');
define('DB_PASSWORD', 'Ihavenopassword');
define('DB_NAME', 'mytablename');
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
//$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if(!$mysqli){
die("Connection failed: " . $mysqli->error);
}
//query to get data from the table
$query = sprintf("SELECT playerid, score FROM score ORDER BY playerid");
//execute query
$result = $mysqli->query($query);
//loop through the returned data
$data = array();
foreach ($result as $row) {
$data[] = $row;
}
//free memory associated with result
$result->close();
//close connection
$mysqli->close();
//now print the data
print json_encode($data);
答案 0 :(得分:0)
$result
不是您所期望的。您需要使用fetch_assoc
获取每个项目,例如:
//loop through the returned data
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}