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

kknvjkwl  于 2022-10-22  发布在  Mysql
关注(0)|答案(1)|浏览(239)

我正在尝试从数据库中获取数据,但遇到了此错误。
致命错误:未捕获错误:调用E:\xamp\htdocs\PoliceApp\News\fetch中未定义的函数mysql_select_db()。php:10堆栈跟踪:E:\xamp\htdocs\PoliceApp\News\fetch中抛出#0{main}。第10行的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);  
?>
vom3gejh

vom3gejh1#

它应该是mysqli_select_db($dbhandle, "police"),其他mysql_*函数也应该更改为mysqli_*

<?php

$username = "root";
$password = "namungoona";
$hostname = "localhost";

// enable error reporting
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// connection string with database
$dbhandle = mysqli_connect($hostname, $username, $password);

// connect with database
$selected = mysqli_select_db($dbhandle, "police");

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

相关问题