【发布时间】:2017-11-05 19:14:48
【问题描述】:
我设置了我的本地主机,我已经准备好了所有相关文件。我在运行 sql 查询的地方运行 php,它返回 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);
【问题讨论】: