ThaiAn Posted May 5, 2013 Share Posted May 5, 2013 (edited) 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 Edited May 5, 2013 by ThaiAn Quote Link to comment https://forums.phpfreaks.com/topic/277640-can-not-connect-to-database/ Share on other sites More sharing options...
trq Posted May 5, 2013 Share Posted May 5, 2013 Remove the error suppressors so that you might actually see the proper error. Quote Link to comment https://forums.phpfreaks.com/topic/277640-can-not-connect-to-database/#findComment-1428304 Share on other sites More sharing options...
ThaiAn Posted May 5, 2013 Author Share Posted May 5, 2013 i removed '@' at : if(!mysql_connect($mysql_host,$mysql_user,$mysql_pass) and !mysql_select_db($mysql_db)) it still output 'No database selected' Quote Link to comment https://forums.phpfreaks.com/topic/277640-can-not-connect-to-database/#findComment-1428306 Share on other sites More sharing options...
androidd Posted May 5, 2013 Share Posted May 5, 2013 mysqli_connect maybe? or something like: $db_connect = mysqli_connect($host, $user, $pass, $db); if (!$db_connect){ echo "Failed" . mysqli_error(); } Kinda new at this myself but this worked for me. Quote Link to comment https://forums.phpfreaks.com/topic/277640-can-not-connect-to-database/#findComment-1428321 Share on other sites More sharing options...
DavidAM Posted May 5, 2013 Share Posted May 5, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277640-can-not-connect-to-database/#findComment-1428398 Share on other sites More sharing options...
ThaiAn Posted May 6, 2013 Author Share Posted May 6, 2013 thanks i got it Quote Link to comment https://forums.phpfreaks.com/topic/277640-can-not-connect-to-database/#findComment-1428522 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.