Joe Torraca
Joe Torraca

Reputation: 2018

No Database Selected - 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);
?>

Upvotes: 10

Views: 74147

Answers (2)

h4ck3rm1k3
h4ck3rm1k3

Reputation: 2100

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

 select * from database.table

Upvotes: 22

Michael Berkowski
Michael Berkowski

Reputation: 270617

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.

Upvotes: 18

Related Questions