Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Database Selected - PHP & MySQL

Tags:

php

mysql

I am trying to connect to my database and when I run the code, I get an error. Can anybody tell me what is wrong also any errors in my PHP code? Thanks.

Error: No database selected

PHP Code:

include('.conf.php');
$prof = $_GET['profile'];
$prof = addslashes(htmlentities($prof));
$query = "SELECT * FROM aTable where id = '".mysql_real_escape_string($prof)."'";
$info_array = mysql_query($query, $con) or die (mysql_error()).;

while($row = mysql_fetch_array( $info_array )) 
{
    echo $row['num1'];
    echo "</br>";
    echo $row['num2'];
    echo "</br>";
    echo $row['num3'];
    echo "</br>";
    echo $row['num4'];
};

mysql_close($con);

.conf.php file:

<?php
    $conf['db_hostname'] = "xxx";
    $conf['db_username'] = "xxx";
    $conf['db_password'] = "xxx";
    $conf['db_name'] = "xxx";
    $conf['db_type'] = "mysql";

    $con = mysql_connect('xxx', 'xxx', 'xxx') or die (mysql_error());
    $db  = mysql_select_db("aTable", $con);
?>
like image 496
Joe Torraca Avatar asked Dec 15 '11 00:12

Joe Torraca


People also ask

What is the meaning of no database selected?

MySQL no database selected is an error that occurs when you execute a statement without first selecting a database. The database may be completely missing, or you may choose the wrong database if there is more than one database.

Could not run statement 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.


2 Answers

I had that problem and solved it with prefixing the table name with the database name, so

 select * from database.table
like image 98
h4ck3rm1k3 Avatar answered Oct 15 '22 23:10

h4ck3rm1k3


Unless you have the password incorrect and need to fix it, run a GRANT statement to grant access to your database user:

GRANT ALL PRIVILEGES ON aTable.* TO xxx@localhost IDENTIFIED BY 'password_for_xxx';

The above grants all privileges. It's often better to restrict to only what's needed. For example, if you only intend to SELECT, but not modify data,

GRANT SELECT ON aTable.* TO xxx@localhost IDENTIFIED BY 'password_for_xxx';

Update

Since you have identified the database name as dedbmysql, change your mysql_select_db() call to:

$db = mysql_select_db("dedbmysql", $con);

Following this, the GRANT statements above are probably unnecessary, as your host may have already worked out the correct database permissions.

like image 35
Michael Berkowski Avatar answered Oct 15 '22 23:10

Michael Berkowski