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);
?>
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.
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.
I had that problem and solved it with prefixing the table name with the database name, so
select * from database.table
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';
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With