尝试连接到 PHP 中的 MySQL 数据库时出错
Error trying to connect to MySQL database in PHP
我正在尝试使用 PHP 和数据库做一个小规模的项目,因为我已经离开很久了。
下面的代码从 mysql_select_db 行给我错误消息 "Failed to find or connect with database."。我这辈子都搞不清楚哪里出了问题,因为这个代码示例是我老师直接给的。
<?php
mysql_connect("localhost", "root", "password") or die("Failed to connect with MySQL.");
mysql_select_db("databases/starblind_database") or die("Failed to find or connect with database.");
$query = "SELECT * FROM news ORDER BY Date LIMIT 6";
$result = mysql_query($query) or die("Failed to execute query.");
while($row_slide = mysql_fetch_array($result))
{
$slide_title[] = $row_slide["Title"];
$slide_sdesc[] = $row_slide["Small_desc"];
$slide_image[] = $row_slide["Image"];
}
?>
谢谢大家的帮助!
数据库名不能包含斜杠
databases/starblind_database
使用 mysqli 连接到 db
$con = mysqli_connect("localhost","root","","starblind_database");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Code gives me the error message "Failed to find or connect with
database.
这意味着它找不到您的数据库。
您无需申报 databases/database_name
。
够了
mysql_select_db("starblind_database") or die("Failed to find or connect with database.");
警告:
mysql
已弃用。您需要使用 Mysqli 或 PDO。您的代码容易受到 sql 注入攻击。
使用以下内容:
$con=mysqli_connect("localhost", "root", "password") or die("Failed to connect with MySQL.");
mysql_select_db($con,"databases/starblind_database") or die("Failed to find or connect with database.");
我正在尝试使用 PHP 和数据库做一个小规模的项目,因为我已经离开很久了。
下面的代码从 mysql_select_db 行给我错误消息 "Failed to find or connect with database."。我这辈子都搞不清楚哪里出了问题,因为这个代码示例是我老师直接给的。
<?php
mysql_connect("localhost", "root", "password") or die("Failed to connect with MySQL.");
mysql_select_db("databases/starblind_database") or die("Failed to find or connect with database.");
$query = "SELECT * FROM news ORDER BY Date LIMIT 6";
$result = mysql_query($query) or die("Failed to execute query.");
while($row_slide = mysql_fetch_array($result))
{
$slide_title[] = $row_slide["Title"];
$slide_sdesc[] = $row_slide["Small_desc"];
$slide_image[] = $row_slide["Image"];
}
?>
谢谢大家的帮助!
数据库名不能包含斜杠
databases/starblind_database 使用 mysqli 连接到 db
$con = mysqli_connect("localhost","root","","starblind_database");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Code gives me the error message "Failed to find or connect with database.
这意味着它找不到您的数据库。
您无需申报 databases/database_name
。
够了
mysql_select_db("starblind_database") or die("Failed to find or connect with database.");
警告:
mysql
已弃用。您需要使用 Mysqli 或 PDO。您的代码容易受到 sql 注入攻击。
使用以下内容:
$con=mysqli_connect("localhost", "root", "password") or die("Failed to connect with MySQL.");
mysql_select_db($con,"databases/starblind_database") or die("Failed to find or connect with database.");