Error selecting database "Error in database Selected"

Asked

Viewed 321 times

1

I was making a system of comments, but 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);
?>

1 answer

2

Try this:

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

And use this only if you need switch between the seats:

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 with this is that you’ll have to write a little more):

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

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

Documentation:

Browser other questions tagged

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