PHP Warning Code Error: mysqli_select_db() expects Exactly 2 Parameters, 1 Given

Asked

Viewed 12,297 times

-1

The error of my code is in the Connection to the database, is the following:

mysqli_select_db() expects Exactly 2 Parameters, 1 Given

<?php
   $servidor='localhost';
   $banco='isartes';
   $usuario='root';
   $senha='';
   $conexao=mysqli_connect($servidor,$usuario,$senha);
   if(!($conexao)){
      echo "Conexao Falhada"; exit;
   }
   $selecao = mysqli_select_db($conexao);
   if(!($selecao)){
      echo "Base de dados nao achada";
      exit;
   }
?> 
  • Missed passing the connection to the function. My suggestion is not to use it, and pass the bank name as the fourth argument of mysqli_connect()

  • The Code trex is just below! <? php $server='localhost'; $bank='isartes'; $user='root'; $password='; $connection=mysqli_connect($server,$user,$password); if(!($connected)){ echo "Failed Connection"; Exit; } $selection= mysqli_select_db($connection); if(!($selection)){ echo "Database not found"; Exit; } ?>

1 answer

0


Most Mysqli functions ask for connection as the first argument. The error indicates that two arguments should be passed, but only one was passed.

There are two ways to solve the problem the first is to do what the error says, pass the name of the bank after connection.

The second is not to use this function and pass the database name as the fourth argument of the function mysqli_connect().

First form:

$conexao = mysqli_connect($servidor,$usuario,$senha);
mysqli_select_db($conexao, $banco);

Second form:

$conexao = mysqli_connect($servidor,$usuario,$senha, $banco);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.