Error in mysqli: "expects Exactly 2 Parameters, 1 Given"

Asked

Viewed 1,081 times

0

You’re making that mistake:

Warning: mysqli_select_db() expects Exactly 2 Parameters, 1 Given in

My code is this:

define("HOST", "localhost");
define("USER", "meu-usuario");
define("PASS", "minha-senha");
define("BDNAME", "meu_banco_de_dados");

mysqli_connect(HOST, USER, PASS);
mysqli_select_db(BDNAME)

2 answers

4


According to the function documentation mysqli_select_db, it is necessary to pass two parameters to this function, the first being the Connection link and the second name of the database.

Edit

Connection link is a resource returned by mysqli_connect() or mysqli_init()

Example:

$con = mysqli_connect(HOST, USER, PASS);
mysqli_select_db($con, BDNAME);

2

There should be two parameters to connect like this:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Checa a database
if (mysqli_connect_errno())
  {
  echo "Deu errado mano: " . mysqli_connect_error();
  }

$teste = "Select * from table";

// Change database to "teste "
mysqli_select_db($con,"teste ");

// ...some PHP code for database "test"...

mysqli_close($con);
?>

or via Object like this:

<?php
// Aqui você se conecta ao banco
$mysqli = new mysqli('localhost', 'root', '', 'mydb');
// Executa uma consulta que pega cinco notícias
$sql = "SELECT `id`, `titulo` FROM `noticias` LIMIT 5";
$query = $mysqli->query($sql);
while ($dados = $query->mysqli_fetch_array()) {
  echo 'ID: ' . $dados['id'] . '';
  echo 'Título: ' . $dados['titulo'] . '';
}
echo 'Registros encontrados: ' . $query->num_rows;

Good only for academic information this method has been discontinued in older versions of php.

  • thanks for the help !!

Browser other questions tagged

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