PHP using mysql_select_db: receiving "is not a valid MySQL-Link resource" error
Here is the skinny - I can connect to my database just fine but when I try to access my table inside the database I keep getting thrown this error code:
Warning: mysql_sel开发者_开发技巧ect_db(): supplied argument is not a valid MySQL-Link resource in /var/www/clients/client11/web31/web/update.php on line 11
Here is my update.php code
<?php
$db_name = "X";
$db_pass = "X";
$db_user = "X";
$db_host = "localhost";
$con = mysql_connect($db_host,$db_user,$db_pass) || die(mysql_error());
$select = mysql_select_db('gold_market',$con) || die(mysql_error());
?>
Help. Please.
Try this
<?php
$db_name = "X";
$db_pass = "X";
$db_user = "X";
$db_host = "localhost";
$con = mysql_connect($db_host,$db_user,$db_pass);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$select = mysql_select_db('gold_market',$con) or die(mysql_error());
?>
It looks like the connection is not actually being made. You should check the result of mysql_connect(). It might be easier to use a bit more verbose code. The PHP manual has a good example: http://php.net/manual/en/function.mysql-connect.php
<?php
$db_name = "X";
$db_pass = "X";
$db_user = "X";
$db_host = "localhost";
$con = mysql_connect($db_host, $db_user, $db_pass);
// var_dump($con); // you can uncomment this for debugging.
if (!$con) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($con);
A side note: mysql_select_db is like "USE dbname". It doesn't "access the table inside the database".
I use or die()
not || die()
I don't know if it makes a difference but its worth a shot.
To me it seemed to be the mysql_select_db syntax. Have you tried something like this:
$con = mysql_connect($db_host, $db_user, $db_pass);
if (!$con) {
die('Not connected : ' . mysql_error());
}
$select = mysql_select_db('gold_market', $con);
if (!$select) {
die ('Table unavailable : ' . mysql_error());
}
精彩评论