PDA

View Full Version : help selecting from vb3_user table!


benroles
07-02-2004, 06:51 PM
Hello,

I know what I am doing (I think!) in PHP but I am having real problems reading the VB3_ tables... This code should check if the username exists in the database, but I cannot get it to work. Can anyone please tell me why?

$query = "SELECT * from vb3_user where 1 and username like '$username'";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
echo $rows; //JUST A TEMPORARY THING FOR TESTING
if ($rows>0){
echo('<p><strong><font color="#FF0000">! The username you selected is already being used. Please select another.</font></strong></p>');
}

Please note that I already called a connection to the database in an include:

$db = mysql_connect("localhost","xxxxx","xxxxx");
mysql_select_db("xxxxx",$db);

When I run this script on another table I get back the number of rows through the echo command, but on the vb3_user table I get nothing, not even an 'echo' of $rows (which should be 1 or 0).

PLEASE can somebody explain what the hell is happening?!!

Regards,
Ben.

Andreas
07-02-2004, 06:59 PM
I assume the table name (vb3_user) is correct?


$query = "SELECT * from vb3_user where 1 and username like '$username'";


You should better change this to


$query = "SELECT * FROM vb3_user WHERE username = '$username'";


Also replace


$result = mysql_query($query);


with

$result = mysql_query($query) or die('Query not successful: ' . mysql_error());


to see what's going wrong with the query.

Modin
07-02-2004, 07:42 PM
you get nothing because when there's no result from your query, you should get nothing in your result, and therefore nothing for your rows.

Try using this code instead (also as Kirby said, don't use the 'WHERE 1' and have the 'or die' code)

$query = "SELECT * from vb3_user WHERE username like '$username'";
$result = mysql_query($query) or die('Query not successful: ' . mysql_error());
if ($result){
echo('<p><strong><font color="#FF0000">! The username you selected is already being used. Please select another.</font></strong></p>');
}