www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Checking if row exists

  1. #1
    Join Date
    Nov 2009
    Posts
    97

    resolved [RESOLVED] Checking if row exists

    Hello all, I've been trying to do something with no luck whatsoever, read several google results on it and I can't find a good working answer that makes my code work.

    I need to check if a row exists, so that if it doesn't I create a button that will create the row and if it does exist I create a statement that will allow me to modify it but I can only make the code work at a 50%.

    This is my code:
    PHP Code:
    $catName    $_GET['name'];                            //get the name from URL
    $search     = array('-','%28','%29');                    //look for dashes and hex %28 %29
    $replace    = array(' ','(',')');                        //replace for spaces,( and )
    $cleanName    str_replace($search$replace$catName);    //clean the name so it matches with the one in DB
    $result mysql_query("SELECT * FROM `cp_l2` WHERE `cp_cat_name` = '".$cleanName."' ");
    while(
    $row mysql_fetch_array($result)){
    if(
    mysql_num_rows($result) > 0){echo 'I\'m here';}
    else{echo 
    'I\'m not here';}

    I say it's working at a 50% because if the row exists the code echoes "I'm here", the problem is when the row doesn't exist, it wont echo I'm not here...

    I need some help please, also, if there is a better method please feel free to post it, thanks in advance for any help provided.

  2. #2
    Join Date
    Nov 2002
    Location
    NJ, USA
    Posts
    785
    Did you try dying? and see if there is an error message? If you query is not running, it would be neither, so you may think it is your else and expect to see "I'm not here", when in fact it may be nothing, because the whole while loop is not happening... Just a theory.
    Last edited by Daria; 05-04-2011 at 12:37 PM.

  3. #3
    Join Date
    Nov 2009
    Posts
    97
    I don't wanna die yet lol

    Ok, I have display_errors off for security reasons, I turned it on hoping to see something and nothing...

    Also I changed the query to this:
    PHP Code:
    $result mysql_query("SELECT * FROM `cp_l2` WHERE `cp_cat_name` = '".$cleanName."' ") or die(mysql_error()); 
    But nothing really changed, you think my while is not working right?

  4. #4
    Join Date
    Oct 2010
    Posts
    25
    You need to check mysql_num_rows() before you start fetching your results. Because now if there are no rows it will not go into the while loop and therefore not check num rows.
    Hope that made sense

  5. #5
    Join Date
    Nov 2009
    Posts
    97
    Thanks for the help andx85

    I'm sorry to say I don't know what you mean with checking mysql_num_rows(), I don't consider myself an expert php coder yet, could you please give me an example?

    Thanks.

  6. #6
    Join Date
    Nov 2002
    Location
    NJ, USA
    Posts
    785
    You can either go directly
    Code:
    echo mysql_num_rows($result);
    to see what's there or assign it into a value such as
    Code:
    $number_of_results = mysql_num_rows($result);
    echo $number_of_results;
    - and do it as follows for "while" statement:

    Code:
    $catName    = $_GET['name'];                            //get the name from URL
    $search     = array('-','%28','%29');                    //look for dashes and hex %28 %29
    $replace    = array(' ','(',')');                        //replace for spaces,( and )
    $cleanName    = str_replace($search, $replace, $catName);    //clean the name so it matches with the one in DB
    $result = mysql_query("SELECT * FROM `cp_l2` WHERE `cp_cat_name` = '".$cleanName."' ");
    
    $number_of_results = mysql_num_rows($result);
    
    if( $number_of_results > 0 )
    { 
    echo $number_of_results.' Results Found!';
       while($row = mysql_fetch_array($result))
       {
       echo 'I\'m here';
    
       }
    }
    else
    {
    echo 'There are no results!';
    echo 'I\'m not here';
    }
    Last edited by Daria; 05-04-2011 at 01:07 PM.

  7. #7
    Join Date
    Nov 2009
    Posts
    97
    You guys are awesome and so is this forum, my problem has been solved, thanks so much Daria and andx85

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles