# MySQL Query

**URL:** <https://www.phphelp.com/t/mysql-query/9557>\
**Category:** Beginners - Learning PHP\
**Created:** [December 20, 2006, 8:57pm UTC](https://www.phphelp.com/t/mysql-query/9557 "2006-12-20T20:57:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robula](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/a183cd/32.png) [@Robula](https://www.phphelp.com/u/Robula)\
**Post date:** [December 20, 2006, 8:57pm UTC](https://www.phphelp.com/t/mysql-query/9557/1 "2006-12-20T20:57:08Z")

</div>

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’”.

[code]\<?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’”);

while($row = mysql\_fetch\_array($result))

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

mysql\_close($con);  
?\>[/code]

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. ![:)]()

---

<div class="post-metadata">

**Author:** ![peg110](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/bb73d2/32.png) [@peg110](https://www.phphelp.com/u/peg110)\
**Post date:** [December 21, 2006, 4:21pm UTC](https://www.phphelp.com/t/mysql-query/9557/2 "2006-12-21T16:21:43Z")

</div>

> [@""Robula""](#):
>
> [code]?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’”);
> 
> while($row = mysql\_fetch\_array($result))
> 
> if(mysql\_fetch\_array($result = ‘Robula UK’))  
> {  
> echo $row[‘username’];  
> }  
> else {echo(‘No’);}
> 
> mysql\_close($con);  
> ?\>  
> [/code]

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]

\<?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]

---

<div class="post-metadata">

**Author:** ![Monk](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@Monk](https://www.phphelp.com/u/Monk)\
**Post date:** [December 24, 2006, 3:57am UTC](https://www.phphelp.com/t/mysql-query/9557/3 "2006-12-24T03:57:41Z")

</div>

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]

\<?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); ?\> 

[/php]
