Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-15-2009, 08:12 PM   PM User | #1
Sussex_Chris
Regular Coder

 
Join Date: Apr 2009
Posts: 135
Thanks: 83
Thanked 0 Times in 0 Posts
Sussex_Chris is an unknown quantity at this point
Problem Querying MySQL Result?

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:

PHP Code:
<?php
$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:

PHP Code:
<?php
$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";
}else{
?>
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?
Sussex_Chris is offline   Reply With Quote
Old 04-15-2009, 08:34 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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.

PHP Code:
error_reporting(E_ALL);
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:

Code:
SELECT IFNULL(username, 'NOT FOUND') FROM `members` WHERE `id`=1
Read the manual for more info:

http://dev.mysql.com/doc/refman/5.0/...unction_ifnull
__________________
Fumigator is offline   Reply With Quote
Old 04-16-2009, 01:23 AM   PM User | #3
Sussex_Chris
Regular Coder

 
Join Date: Apr 2009
Posts: 135
Thanks: 83
Thanked 0 Times in 0 Posts
Sussex_Chris is an unknown quantity at this point
Got it to work now Thanks for your help
Sussex_Chris is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:40 AM.


Advertisement
Log in to turn off these ads.