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 7 of 7
  1. #1
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts

    Keeps coming out as true!

    So what I want to do is check if there is a entry for a specified title already in the database, if there is no entry then output the form for it. otherwise just display the 'title' of the entry. Pretty simple i'd say

    There are no mysql_errors();

    The problem is it keeps coming out as "TRUE" and keeps outputting an empty 'title' or the listed title. Please help.

    PHP Code:
    <?php
    $query 
    "SELECT title FROM entry WHERE entry_id='$id' AND type='$type'";
    $result mysql_query($query);
    $data mysql_fetch_array($result);

    if(!
    $result)
    {
    ?>
        <form>
            The form
        </form>
    <?php
    }
    else

        echo 
    $data['title'];
    }
    ?>
    SOLUTION:

    PHP Code:
    <?php
    $query 
    "SELECT title FROM entry WHERE entry_id='$id' AND type='$type'";
    $result mysql_query($query);
    $data mysql_fetch_array($result);

    // Add this
    $num mysql_num_rows($result);
    //Edit this
    if($num == 1)
    {
    ?>
        <form>
            The form
        </form>
    <?php
    }
    else

        echo 
    $data['title'];
    }
    ?>
    Last edited by Dat; 09-15-2009 at 12:12 AM.

  • #2
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    You forgot to do the query

    Edit: You also don't get any data...

    PHP Code:
    <?php
    $query 
    "SELECT title FROM entry WHERE entry_id='$id' AND type='$type'";
    $result mysql_query($query) or die(mysql_error()); /// <--- add that line

    if(!$result)
    {
    ?>
        <form>
            The form
        </form>
    <?php
    }
    else

        
    $data mysql_fetch_array($resultMYSQL_ASSOC); // <---- That line, too
        
    echo $data['title'];
    }
    ?>

  • #3
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bacterozoid View Post
    You forgot to do the query

    Edit: You also don't get any data...

    PHP Code:
    <?php
    $query 
    "SELECT title FROM entry WHERE entry_id='$id' AND type='$type'";
    $result mysql_query($query) or die(mysql_error()); /// <--- add that line

    if(!$result)
    {
    ?>
        <form>
            The form
        </form>
    <?php
    }
    else

        
    $data mysql_fetch_array($resultMYSQL_ASSOC); // <---- That line, too
        
    echo $data['title'];
    }
    ?>
    I did i edited before you posted. I forgot, but it's there. Just assume I made a data connection already.
    Last edited by Dat; 09-14-2009 at 09:43 PM.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    A query that returns zero rows is a successful query. $result will be a result resource with zero rows in it.

    $result will only be FALSE if the query fails to execute (usually due to syntax errors...)

    You need to use mysql_num_rows() to find out how many rows there are in the result set.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    A query that returns zero rows is a successful query. $result will be a result resource with zero rows in it.

    $result will only be FALSE if the query fails to execute (usually due to syntax errors...)

    You need to use mysql_num_rows() to find out how many rows there are in the result set.
    Thank you

    SOLUTION:

    PHP Code:
    <?php
    $query 
    "SELECT title FROM entry WHERE entry_id='$id' AND type='$type'";
    $result mysql_query($query);
    $data mysql_fetch_array($result);

    // Add this
    $num mysql_num_rows($result);
    //Edit this
    if($num == 1)
    {
    ?>
        <form>
            The form
        </form>
    <?php
    }
    else

        echo 
    $data['title'];
    }
    ?>
    Last edited by Dat; 09-15-2009 at 12:12 AM.

  • #6
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    That's backwards of what you originally said.

    if there is no entry then output the form for it
    You would want to use

    PHP Code:
    if($num == 0// Notice the ==, not =. You need to do a comparison, not an assignment. 

  • #7
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bacterozoid View Post
    That's backwards of what you originally said.



    You would want to use

    PHP Code:
    if($num == 0// Notice the ==, not =. You need to do a comparison, not an assignment. 
    Hehe good catch sorry bout that, I edit the code for easier understanding on the forum.


  •  

    Posting Permissions

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