10

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);
?>
8
  • 3
    mysql_select_db("aTable", $con); is aTable the name of your DB? sounds like a table name for some reason:) Commented Dec 15, 2011 at 0:04
  • 1
    Try putting an or die (mysql_error()); on the select_db() line to see if it outputs any errors. Commented Dec 15, 2011 at 0:04
  • Have you checked the return value of mysql_select_db() (i.e. $db) whether it is FALSE? Commented Dec 15, 2011 at 0:04
  • @JamWaffles the error is Access denied for user 'xxx'@'%' to database 'aTable' Commented Dec 15, 2011 at 0:07
  • @JosephTorraca There you have it then - you need to grant access to user xxx on database aTable Commented Dec 15, 2011 at 0:10

2 Answers 2

22

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

 select * from database.table
Sign up to request clarification or add additional context in comments.

Comments

18

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.

3 Comments

I just tried to run it on PHPmyAdmin and it gives me this error: #1044 - Access denied for user 'xxx'@'%' to database 'xxx'
@JosephTorraca What is the actual name of your database? aTable is clearly the name of the table, but the database must have a different name.
@JosephTorraca See change above then. You need to fix mysql_select_db()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.