我需要将我的项目从MySQL
转换为MySQLIi
,但我在配置文件中遇到问题,请问我的代码中的错误是什么?
<?PHP
$Db = array (
"hostname"=>"localhost",
"dbname"=>"prstitodb",
"dbuser"=>"root",
"dbpass"=>"",
);
$Dbconnect = mysqli_connect($Db['hostname'],$Db['dbuser'],$Db['dbpass']) or die(mysqli_error());
$DbSelect = mysqli_select_db($Db,['dbname']) or die(mysqli_error());
?>
答案 0 :(得分:2)
错误消息告诉您传递的第一个参数是一个数组,根据文档,mysqli::select_db
期望第一个参数是您的数据库连接。见http://php.net/manual/en/mysqli.select-db.php
$DbSelect = mysqli_select_db($Dbconnect, $Db['dbname']) or die(mysqli_error());
答案 1 :(得分:1)
mysqli_select_db
询问了两个参数,其中第一个参数是连接对象,而您只提供了一个参数,而且是一个数组: -
$DbSelect = mysqli_select_db($Db,['dbname']) or die(mysqli_error());
,
您需要先将其删除,然后提供$Dbconnect
作为第一个参数
要解决此错误,只需将您的代码简化为一行,如下所示: -
<?PHP
$Dbconnect = mysqli_connect('localhost','root','','prstitodb') or die(mysqli_error());
?>
但如果您仍想按照自己的方式行事,请删除,
,这样就可以了
<?PHP
$Db = array (
"hostname"=>"localhost",
"dbname"=>"prstitodb",
"dbuser"=>"root",
"dbpass"=>"",
);
$Dbconnect = mysqli_connect($Db['hostname'],$Db['dbuser'],$Db['dbpass']) or die(mysqli_error());
$DbSelect = mysqli_select_db($Dbconnect,$Db['dbname']) or die(mysqli_error($Dbconnect)); // remove `,` and provide connection object as first parameter
?>
答案 2 :(得分:0)
你的代码错了,试试这个:
<?PHP
$Db = array (
"hostname"=>"localhost",
"dbname"=>"wordpress",
"dbuser"=>"root",
"dbpass"=>"",
);
$Dbconnect = mysqli_connect($Db['hostname'],$Db['dbuser'],$Db['dbpass']) or die(mysqli_error($Dbconnect));
$DbSelect = mysqli_select_db( $Dbconnect,$Db['dbname']) or die(mysqli_error($Dbconnect));
if(!$Dbconnect){
die("not connected");
}else{
echo "connected";
}
?>