Re: mysql_select_db problem
| From: | Philip Olson | Date: | Tue, 04 Jun 2002 02:52:21 +0000 |
| Subject: | Re: mysql_select_db problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-100504@lists.php.net to get a copy of this message | ||
> The following code always returns the "Couldn't select
> database" error.
> <?
> $db_name="mydb";
> $table_name="my_table";
> $connection = @mysql_connect("localhost", "user", "password") or
> die
> ("Couldn't connect.");
> $db = @mysql_select_db($db_name) or die ("Couldn't select database");
> ?>
Add some useful debugging:
if (!$conn = mysql_connect($host, $user, $pass)) {
print "Cannot connect to DB : " . mysql_error();
exit;
}
if (!@mysql_select_db($dbname)) {
print "Cannot select DB ($dbname) : " . mysql_error();
exit;
}
The key here is mysql_error(). You could also use this
function within your 'or die()' but personally I frown
upon using 'or' like this, it's rather limiting.
In the above, we are checking if the function returns false
and if it does we do stuff, like print mysql_error() and
exit the script. Do as you wish.
On a related note, if you removed the '@' from your code,
odds are it'd shout an error too. '@' can be useful for
supressing errors so we can implement our own form of
error management/trapping, like in the above.
Regards,
Philip Olson