Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No database selected" error even after a db is selected

Tags:

php

mysql

I have selected the database but for some weird reason it still says that it is not selected.

Connection lines:

$location = "localhost";
$user = "user";
$pass = "pass";

//Database Selection
$link = mysql_connect($location, $user, $pass); 
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
//Database Selection
mysql_select_db('database') or die(mysql_error());

The Query:

while ($row_fpages = mysql_fetch_array($result_fanpage))
{
    if ( $row_fpages['client'] == NULL ) {

    //GRAPHS
    $sql = "SELECT date, likes FROM statistics_pages WHERE idnum='".$_COOKIE['id']."' AND page_name = ".$row_fpages['page_name']." LIMIT 7";
    $result_apps = mysql_query($sql) or die(mysql_error());

And the error is a plain No database selected.

I have never seen this error before and i tried to change a lot of things but its just not working.

like image 801
Ricardo Avatar asked Nov 02 '11 23:11

Ricardo


People also ask

What is mean by no database selected error in MySQL?

MySQL No Database Selected Error CausesThis error will mostly happen if you are trying to create a table in the MySQL database using the command prompt. While executing a command from the command prompt, you must also select the database. Otherwise, MySQL will not know from which database you are running the script.

How do I fix error no database selected?

The error no database selected frequently occurs in MySQL when you perform a statement without selecting a database first. You need to replace [database_name] with the name of a database that exists in your MySQL server.

How do I fix #1046 No database selected?

Learn MySQL from scratch for Data Science and Analytics Now, we can choose any database. Suppose I am using the database 'business', therefore we can choose with the help of 'use' command. After using database 'business', we can create the above table and we will not get any error.


1 Answers

You forgot to pass the variable $link as the link parameter.

     mysql_select_db('database', $link) or die(mysql_error());

EDIT: Try passing the database in the FROM parameter like

     SELECT * FROM `database`.`table`
like image 71
David Bélanger Avatar answered Nov 15 '22 11:11

David Bélanger