Mutaz Ghosheh February 2016

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

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

?>

Answers


Anant February 2016

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

?>


Kisaragi February 2016

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


rdn87 February 2016

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


?>

Post Status

Asked in February 2016
Viewed 2,870 times
Voted 11
Answered 3 times

Search




Leave an answer