Jump to content

can not connect to database


ThaiAn

Recommended Posts

i have a php file

 <?php$mysql_host='localhost';$mysql_user='root';$mysql_pass='root';$mysql_db='b_database';  if(!@mysql_connect($mysql_host,$mysql_user,$mysql_pass) and !@mysql_select_db($mysql_db)) { echo 'error';}else{    if($query=mysql_query("SELECT `food`,`color`  FROM `food` ORDER BY `id`"))    {echo 'ok';    }    else    {    echo mysql_error();    }} ?>
 

 

 when i run the code it always says that no database selected. even i had created a it's database.Sorry for my bad english

Link to comment
https://forums.phpfreaks.com/topic/277640-can-not-connect-to-database/
Share on other sites

Have a look at the mysql_error() when the connection OR the select db fails. If you care going to do it that way (in a single IF statement) it needs to be OR not AND.

 

if(!mysql_connect($mysql_host,$mysql_user,$mysql_pass) OR !mysql_select_db($mysql_db)){
  echo 'error: ' . mysql_error();
}
with AND, since you are looking for failure, you only get that error condition if BOTH fail. It would appear that the connect succeeds but the select fails.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.