您好我是PHP的新手,我编写了一个php页面并创建了一个数据库。我试图连接数据库,但它没有连接。
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
mysql_close($mysqlconn);
?>
答案 0 :(得分:0)
您正在混合使用mysql和mysqli
你必须替换
/connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
使用此代码
$mysqlconn = mysqli_connect($hostn, $uname, $pwd,"dataforuse")
or die("Unable to connect to MySQL");
答案 1 :(得分:0)
嗨尝试调试和回应与mysqli_error()
function
相关的错误。
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd) or die(mysqli_connect_error());
or
$mysqlconn = mysqli_connect($hostn, $uname, $pwd , $database) or die(mysqli_connect_error());
答案 2 :(得分:0)
连接变量然后数据库名称..这个
$dbselect = mysqli_select_db($mysqlconn,"dataforuse");
和mysql关闭应该是这样的
mysqli_close($mysqlconn);
不要将mysql与mysqli混合
答案 3 :(得分:0)
你应该写
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
$dbname = "dataforuse";
$conn = new mysqli($hostn, $uname, $pwd, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
这将解决您的问题