我正试图使用 Xamp Server 从数据库中获取数据,但却出现了这样的错误。
致命错误:Uncaught Error:调用未定义的函数 mysql_select_db() in E:\xamp\htdocs\PoliceApp\News\fetch.php:10 Stack trace:#0 {main} thrown in E:\xamp\htdocs\PoliceApp\News\fetch.php on 第 10 行
下面是我的php脚本,我还是php的新手,请帮助我。 但我读了这里所有其他的帖子,但似乎对我来说很困惑,请问我怎样才能把它做好。
<?php
$username="root";
$password="namungoona";
$hostname = "localhost";
//connection string with database
$dbhandle = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "";
// connect with database
$selected = mysql_select_db("police",$dbhandle)
or die("Could not select examples");
//query fire
$result = mysql_query("select * from News;");
$json_response = array();
// fetch data in array format
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// Fetch data of Fname Column and store in array of row_array
$row_array['Headlines'] = $row['Headlines'];
$row_array['Details'] = $row['Details'];
$row_array['NewsPhoto'] = $row['NewsPhoto'];
//push the values in the array
array_push($json_response,$row_array);
}
//
echo json_encode($json_response);
?>
根据您的要求,我修改了代码。
<?php
$username="root";
$password="namungoona";
$hostname = "localhost";
//connection string with database
$dbhandle = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "";
// connect with database
$selected = mysqli_select_db($dbhandle, "police")
or die("Could not select examples");
//query fire
$result = mysqli_query($dbhandle,"select * from News;");
$json_response = array();
// fetch data in array format
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// Fetch data of Fname Column and store in array of row_array
$row_array['Headlines'] = $row['Headlines'];
$row_array['Details'] = $row['Details'];
$row_array['NewsPhoto'] = $row['NewsPhoto'];
//push the values in the array
array_push($json_response,$row_array);
}
//
echo json_encode($json_response);
mysqli_free_result($result);
?>
请注意:您需要添加错误检查功能。另请注意,我只是在这里输入(未经测试),如果有错误,请见谅。