mysqli_select_db() expects parameter 1 to be mysqli, array given [duplicate]

廉价感情. 提交于 2020-01-07 08:37:07

问题


i need to to convert my project from MySQL to MySQLIi ,but i have problem in config file, please what is the error in my code?

<?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()); 

?>

回答1:


mysqli_select_db() asked two parameters in which first parameter will be connection object and you provided only one parameter and that is an array :-**

$DbSelect = mysqli_select_db($Db,['dbname']) or die(mysqli_error()); 

, you need to remove this first and then provide $Dbconnect as first parameter

To resolve the error, just simplify your code in 1 line like below:-

<?PHP
   $Dbconnect = mysqli_connect('localhost','root','','prstitodb') or die(mysqli_error());
?>

But if you still want to go with your way then remove , and it's ok

<?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:


The error message is telling you that the first parameter you are passing is an array, according to the docs, mysqli::select_db expects the first param to be your db connection. See http://php.net/manual/en/mysqli.select-db.php

$DbSelect = mysqli_select_db($Dbconnect, $Db['dbname']) or die(mysqli_error()); 



回答3:


ur code is wrong, try this:

<?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";
   }


?>


来源:https://stackoverflow.com/questions/35274804/mysqli-select-db-expects-parameter-1-to-be-mysqli-array-given

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!