Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2009
    Posts
    135
    Thanks
    83
    Thanked 0 Times in 0 Posts

    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?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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

  • #3
    Regular Coder
    Join Date
    Apr 2009
    Posts
    135
    Thanks
    83
    Thanked 0 Times in 0 Posts
    Got it to work now Thanks for your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •