Xamp Serverを使用してデータベースからデータを取得しようとしているのですが、以下のようなエラーが発生します。
フェイタルエラーです:Uncaught Error:未定義の関数への呼び出し mysql_select_db() in E:╱htdocs↩PoliceApp↩NewsFetch.php:10 Stack トレースします:#0 {main} thrown in E:\xamphtdocs}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);
?>
注意:エラーチェックを追加する必要があります。また、注意はここに入力しただけです(テストしていません)ので、多少の間違いがあってもご容赦ください。