> 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


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to