Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught Error: Call to undefined function mysql_select_db()

Tags:

php

mysql

mysqli

I'm trying to fetch data from the database but I am getting this error.

Fatal error: Uncaught Error: Call to undefined function 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 line 10

How can I make this right?

<?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);  
?>  
like image 701
Lutaaya Huzaifah Idris Avatar asked Nov 30 '22 15:11

Lutaaya Huzaifah Idris


1 Answers

It should be mysqli_select_db($dbhandle, "police") and other mysql_* functions should be changed to their mysqli_* as well.

<?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);
like image 196
bansi Avatar answered Dec 06 '22 11:12

bansi