mySQL无法建立与php的连接[重复]

时间:2022-09-22 16:03:04

This question already has an answer here:

这个问题在这里已有答案:

I created a very simple database on mySQL using phpMyAdmin. Now I want to connect my database through php file so that I can manipulate data in DB, i.e. inserting, deleting, showing, updating data. I wrote the code:

我使用phpMyAdmin在mySQL上创建了一个非常简单的数据库。现在我想通过php文件连接我的数据库,以便我可以操纵数据库中的数据,即插入,删除,显示,更新数据。我写了代码:

index.php file:

    <?php
include 'includes/connection.php';
$query="SELECT * FROM people";
$result=mysql_query($query);
while($person= mysql_fetch_array($result)){
echo "<h3>". $person['name']."</h3>";

}
?>

connection.php file:

    <?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$db='mysql_tut';
$conn=mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);

?>

But it shows the following error in the browser window:

但它在浏览器窗口中显示以下错误:

 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\phpMyAdmin\index.php on line 5

1 个解决方案

#1


0  

You gotta pass two parameters to the function. The database name and the query.

你必须将两个参数传递给该函数。数据库名称和查询。

$conn = mysqli_connect($query, $query);

I use mysqli_connect instead of mysql_connect becaouse mysql is deprecated.

我使用mysqli_connect而不是mysql_connect,因为不推荐使用mysql。

#1


0  

You gotta pass two parameters to the function. The database name and the query.

你必须将两个参数传递给该函数。数据库名称和查询。

$conn = mysqli_connect($query, $query);

I use mysqli_connect instead of mysql_connect becaouse mysql is deprecated.

我使用mysqli_connect而不是mysql_connect,因为不推荐使用mysql。