View Full Version : Problem Querying MySQL Result?

04-15-2009, 09:12 PM
I have already got a fully functional membership site and I wanted to add in a query to see if the user has changed the password from the default (Use amember pro as well and link across domains so must be seperate).

I tried the following to just echo the users username and it worked:

$result=mysql_query("SELECT `username` FROM `members` WHERE `id`=1");
while ($row = mysql_fetch_array($result)) {
$USER = $row["username"];
echo $USER;

(Each user has their own seperate database so I only need so search where id=1).

This displays the default username.

So I want to query this username so I change this code to:

$result=mysql_query("SELECT `username` FROM `members` WHERE `id`=1");
while ($row = mysql_fetch_array($result)) {
$USER = $row["username"];
if $USER==null{
echo "Please Change Your Username";

Now the page wont load at all. At the very end of the page I have included <?php } ?>

Really what I would like to do is query the MySQL result so if $USER="someusername" rather than NULL.

Anyone know if this is possible or how to fix my code? :)

04-15-2009, 09:34 PM
Your PHP is throwing a parsing error that you aren't configured to display. Change you error reporting so you can see the parsing error first and foremost.

ini_set("display_errors", 1);

Now you will the the problem; your IF statement's condition must be enclosed in parenthesis.

On to your actual goal, to return a row if username is NULL (or not null, either way). Add WHERE username IS NULL to the query.

This will return zero rows if the username isn't null, so you just need to check the count of rows returned with mysql_num_rows(), you don't even have to fetch anything to get what you need.

If you will be fetching the resultset anyway, then there's another way you can do it. Use the IFNULL() MySQL function. This function gives you the opportunity to specify an alternate value if a column is null, and the column's value if a column isn't null. Example:

SELECT IFNULL(username, 'NOT FOUND') FROM `members` WHERE `id`=1

Read the manual for more info:


04-16-2009, 02:23 AM
Got it to work now :) Thanks for your help :)