Error selecting bank "Error in database selected" [closed]

1

I was doing a comment system, but it is giving error:

  

Error in database selected

Code:

<?php
$link=mysqli_connect("127.0.0.1","root","");
$banco=mysqli_select_db($link,"bdcomentario");
?>
<form name='form' method="post" action="#">
Nome: 
<input type='text' name='nome'>
<br><br> E-mail:
<input type='email' name='email'>
<br><br>
Comentario:
<textarea name='coment'></textarea>
<br><br>
<input type='submit' value='Enviar'>
<input type="reset" value='Limpar'>
</form>
<?php 
$nome=$_POST['nome'];
$email=$_POST['email'];
$data= date("d/m/Y");
$comentario=$_POST['coment'];
$st="INSERT INTO tbcomentarios(nome,email,data,coment) 
values('$nome','$email','$data','$comentario')";
if(strlen($_POST['nome'])){
$insert= mysqli_query($link,$vrau);
}
$sql="SELECT * FROM tbcomentarios ORDER BY id desc";
$executar=mysqli_query($link, $sql)
or die("Error: ".mysqli_error($link));
while( $exibir=mysqli_fetch_assoc($executar)){
echo $exibir['data'];
echo '<br>';
echo $exibir['nome'];
echo '<br>';
echo $exibir['email'];
echo '<br>';
echo $exibir['coment'];
echo '<br>';
mysqli_free_result($executar);
}
mysqli_close($link);
?>
    
asked by anonymous 10.11.2016 / 20:13

1 answer

2

Try this:

$link = mysqli_connect("127.0.0.1", "root", "", "bdcomentario");

And use this only if you need to switch between banks:

mysqli_select_db($link, "OUTRO BANCO");

An example:

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "test");

/* verifica conexão */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* retorna o nome do banco atual */
if ($result = mysqli_query($link, "SELECT DATABASE()")) {
    $row = mysqli_fetch_row($result);
    printf("Default database is %s.\n", $row[0]);
    mysqli_free_result($result);
}

/* Troca de banco */
mysqli_select_db($link, "world");

if ($result = mysqli_query($link, "SELECT DATABASE()")) {
    $row = mysqli_fetch_row($result);
    printf("Default database is %s.\n", $row[0]);
    mysqli_free_result($result);
}

mysqli_close($link);

You can pass the name of the bank next to SELECT (the only problem of this is that you will have to write some more):

$link = mysqli_connect("127.0.0.1", "root", "", "bdcomentario");

mysqli_query($link, "SELECT * FROM bdcomentario.tabela WHERE ...");

Documentation:

10.11.2016 / 20:28