I never understand why people come up with their own error messages.
Instead of:
$link = mysql_connect( "localhost:/users/rupertbj/domains/rupstar.co.uk/sql/mysql.sock", "root" );
if ( !$link ) die( "Couldn't connect to MySQL" );
mysql_select_db( $db, $link ) or die( "Couldn't open $db: ".mysql_error() );
$result = mysql_query("SELECT user FROM userTable WHERE user = $user", $link);
$num_rows = mysql_num_rows($result);
do:
// use these two utility functions to help you out
function myError($message = "", &$dbLink)
{
if($dbLink != "")
{
echo "MySQL said: " . mysql_error($dbLink) . "<br>";
}
die(print( $message));
}
function echeckDB($message = "", &$dbLink)
{
if(mysql_error($dbLink) != "")
{
myError($message, &$dbLink);
}
}
// set connection values before hand makes it more readable
$link = mysql_connect($dbHost, $dbUser, $dbPass);
echeckDB("Could not connect to database, sorry.", &$link);
mysql_select_db($dbName, $link);
echeckDB("Could not select correct database, sorry.", &$link);
// notice how I have surrounded the variable with single quotes
//in the string? This may very well solve the problem by itself.
// Try it with and without and see what happens.
$sqlSelectOneUser = "SELECT user FROM userTable WHERE user = '" . $user . "'";
$result = mysql_query($sqlSelectOneUser, $link);
// ALWAYS check for errors after a query.
echeckDB("Could not find user, sorry.", &$link);
$recordcount= mysql_num_rows($result);
Tip 1: ALWAYS use provided error messages when debugging. (in this case using mysql_error())
Tip 2: Never assume a query was successful.
Tip 3: Surround values with a " or a '.