MySQL Query


Hi all, I just need some help with a code I have written. Sorry if it’s complete gibberish (only started learning 3 weeks ago!).

Basically, all I want to do is write a script that will query a MySQL database to find a user called ‘Robula UK’. This works so far and the browser outputs ‘Robula UK’ as it should.

I now want to output a message saying something like ‘Record not found’ only if ‘Robula UK’ is not found in that MySQL table.

So, a script that asks “Does this user exist? If so output ‘Robula UK’. If not then output ‘Record not found’”.


$con = mysql_connect(“localhost”,“root”,“censored”);
if (!$con)
die('Could not connect: ’ . mysql_error());

mysql_select_db(“forum”, $con);

$result = mysql_query(“SELECT * FROM phpbb_users WHERE username=‘Robula UK’”);

while($row = mysql_fetch_array($result))

if(mysql_fetch_array($result = ‘Robula UK’))
echo $row[‘username’];
else {echo(‘No’);}


You can see my attempts…I have tried and tried. But each time I fail to find the right syntax for this. Any help would be greatly appreciated. :)


the mysql_fetch_array returns an ARRAY and you are trying to access the array as a single variable when you need to check the Array element .

<?php $con = mysql_connect("localhost","root","censored"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("forum", $con); $result = mysql_query("SELECT * FROM phpbb_users WHERE username='Robula UK'"); // You would also need to bracket your WHILE loop if you had more than // one command (in this case control sturcture) in the WHILE loop. while($row = mysql_fetch_array($result)) // Here is/was the problem. You need to get the data from the array element. //Since the query was a SELECT * you got every field as a part of the array. //In the current ROW you can access EACH field as an element in the array. // I assumed that the field name was UserName but of course you would use // whatever the field name was actually called. if(mysql_fetch_array($result['UserName'] = 'Robula UK')) { echo $row['username']; } else { // No Parentheses for the ECHO command echo 'No'; } mysql_close($con); ?>[/php]


Well, since in this case you are only looking for one name, all you need to know is if mysql_fetch_array() is empty or not. If it is empty then you didn’t find the name, so based on your code I think you want something along these lines…


<?php // Create database connection $con = mysql_connect("localhost","root","censored"); if (!$con) { die('Could not connect: ' . mysql_error()); } // Select database mysql_select_db("forum", $con); // Get all rows where the username = 'Robula UK' // This had better only return 1 row or 0 rows $result = mysql_query("SELECT * FROM phpbb_users WHERE username='Robula UK'"); // Set $row to be the first row returned. If no // rows were found then $row == FALSE $row = mysql_fetch_array($result) // If $row is false... if( !$row ) echo 'User not found.'; else echo 'Robula UK'; // Otherwise we found our user (because there was a result) mysql_close($con); ?>