我正在创建一个My SQL数据库,我创建了一个名为login
的数据库和表名login1
。每次我尝试加载页面时都会出现错误unable to select database
,我不明白。在我的config.php的最后一行中,它说:
$db = mysql_select_db($SETTINGS["login"], $connection)
or die ('request "Unable to select database."');
?>
我看不出我在代码中犯过的任何其他错误。 感谢
答案 0 :(得分:2)
您必须像这样连接到服务器。
// database connection info
$conn = mysql_connect("localhost","user","pass") or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db("databae",$conn) or trigger_error("SQL", E_USER_ERROR);
或更好地使用PDO或MySQLi。简单的例子:
$con = mysqli_connect("localhost", "username", "password") or die("Connection Problem" . mysqli_errno($con));
$database = mysqli_select_db($con, "database_name") or die("SQL Problem" . mysqli_error($con));
或者使用PDO:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>