未捕获的错误:调用未定义的函数mysql_select_db()

时间:2016-11-17 09:10:43

标签: php

我正在尝试使用Xamp Server从数据库中获取数据,但我收到此错误。

  

致命错误:未捕获错误:调用未定义的函数   E:\ xamp \ htdocs \ PoliceApp \ News \ fetch.php中的mysql_select_db():10 Stack   trace:#0 {main}抛出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);  
?>  

2 个答案:

答案 0 :(得分:14)

根据您的要求,我修改了代码。

<?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);
?>

请注意:您需要添加错误检查。另请注意只需键入此处(未经测试),如果有错误,请耐心等待。

答案 1 :(得分:0)

只需在php代码中将mysql更改为myqsli,因为XAMPP(我想)正在正确读取SQLi而不是SQL