我在尝试连接数据库时遇到了一些问题。 我使用了以下代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Playlists</title>
<meta name="description" content="test">
<meta name="author" content="SitePoint">
<link rel="stylesheet" href="stylesheets.css">
</head>
<body>
<nav>
<a href="index.html"><img src="http://www.samsung.com/us/showcase/milk/img/meta-fb-milk-music.png" alt="Home"></a>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="">Albums</a></li>
<li><a href="playlist.html">Playlists</a></li>
<li><a href="">About</a></li>
</ul>
</nav>
<div class="content">
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("not fire" . $conn->connect_error);
}
echo "fire";
@mysql_select_db($database) or die( "Unable to select database");
mysql_close();
?>
</div>
</body>
</html>
当然这些不是我真正的登录名,但这些肯定是正确的。 当我打开页面时它会说:“fireUnable to select database”所以与服务器的连接没问题,但是与数据库的连接却没有。有人可以帮我吗?
答案 0 :(得分:0)
您错过了Connection中的数据库名称。您正在使用面向对象的样式,还记得吗?试试这个:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Playlists</title>
<meta name="description" content="test">
<meta name="author" content="SitePoint">
<link rel="stylesheet" href="stylesheets.css">
</head>
<body>
<nav>
<a href="index.html"><img src="http://www.samsung.com/us/showcase/milk/img/meta-fb-milk-music.png" alt="Home"></a>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="">Albums</a></li>
<li><a href="playlist.html">Playlists</a></li>
<li><a href="">About</a></li>
</ul>
</nav>
<div class="content">
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "dbname";
// Create connection
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("not fire: " . $conn->connect_error);
}
echo "fire";
?>
</div>
</body>
</html>