Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10
  1. #1
    Member
    Join Date
    Apr 2004
    Posts
    58
    Member #
    5497
    In PHP, using MySQL, if you use a "SELECT" command and what you are trying to select doesn't exist, won't you get an error? I have:

    PHP Code:
    $check mysql_query("SELECT * FROM members WHERE username=\"$username\" AND password=\"$password\"") or die("<img src=\"images/button.gif\"> Either the username or the password you entered is incorrect. Check them and try again. <a href=\"members.php?p=login\">Back.</a><br /><img src=\"images/button.gif\"> <a href=\"members.php?p=forgot\">Forgotten your password?</a>"); 
    It won't 'die' on anything. Even things that don't exist. So I changed it to:

    PHP Code:
    $check mysql_query("SELECT * FROM members WHERE username=$username AND password=$password") or die("<img src=\"images/button.gif\"> Either the username or the password you entered is incorrect. Check them and try again. <a href=\"members.php?p=login\">Back.</a><br /><img src=\"images/button.gif\"> <a href=\"members.php?p=forgot\">Forgotten your password?</a>"); 
    Now it 'dies' on everything. Even things that do exist.

    What is wrong?

  2.  

  3. #2
    Member
    Join Date
    Apr 2004
    Posts
    58
    Member #
    5497
    Also, I had stored the query in a variable and printed that variable. I copied and pasted it into my MySQL Monitor and it returned the right data, when I used mysql_query() it died.

  4. #3
    Member
    Join Date
    Apr 2004
    Posts
    58
    Member #
    5497
    After that (if it didn't die), I used mysql_fetch_array() to pull information from it. For testing I put, print($valid['name_first']). On the example where everything passes, if I put it my username and password, it prints: Zachary. On things that don't exist it prints nothing. Because it doesn't exist!

  5. #4
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    What's your question?

    If you perform a correct query, but no results match your query, then you won't get an error as no error occured. You get a valid, empty resultset.

    i.e. as long as the columns 'username' and 'password' exists, the query won't generate any error even if no matches are found.

    This:
    SELECT * FROM members WHERE username=$username AND password=$password
    will generate an error, because quotemarks around the values are missing.

    Should be:
    SELECT * FROM members WHERE username='$username' AND password='$password'

    -------
    If you enter a query in mysql monitor, and it works, then it should also work when passing it through PHP. If it doesn't then something in your PHP code is wrong. (That's why you should always add the mysql_error() to pinpoint where the query fails, as discussed in an earlier thread posted by you)

    If your query consists of values beeing derived from an array, you should build your query string such that the string parameters and array elements are concatenated, and not listed as a single string within two quotemarks:

    PHP Code:
    "SELECT * FROM table_name WHERE columName=' " $foor['bar'] . " ' AND blah, blah, blah" 
    S. Rosland

  6. #5
    Member
    Join Date
    Apr 2004
    Posts
    58
    Member #
    5497
    So, what would I do if I wanted to perform certain functions if it returned an empty set, and others if it didn't?

  7. #6
    Member
    Join Date
    Apr 2004
    Posts
    58
    Member #
    5497
    Nevermind I figured it out myself. I did:

    PHP Code:
    if(!mysql_fetch_row($check)){
    die(
    "<img src=\"images/button.gif\"> Either the username or the password you entered is incorrect. Check them and try again. <a href=\"members.php?p=login\">Back.</a><br /><img src=\"images/button.gif\"> <a href=\"members.php?p=forgot\">Forgotten your password?</a>");} 
    After the query. Seems to work.

  8. #7
    Member
    Join Date
    Apr 2004
    Posts
    58
    Member #
    5497
    It's weird but it works:

    When I did the above, for some reason I could no longer 'fetch' from that $check. I could do: $valid = mysql_fetch_array($check); print($valid); I didn't get the output "Array" which you would normally get. So I re-queried and it works now. Any thoughts?

  9. #8
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    Your query will return an empty result set if the user does not exist. Calling mysql_fetch_array() on an empty result set simply yields a return value of false. mysql_fetch_array() also returns false if you have reached the end of a result set too.

    You should use the mysql_num_rows() function to test whether or not a user was found in the database - like so:
    PHP Code:
    if(mysql_num_rows($check) == 0) die ('invalid username or password'); 

  10. #9
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    PHP Code:
    if(mysql_num_rows($check) < 1)
    {
     echo 
    "No matching results found";
    }

    else
    {
     
    //perform the rest of your operations

    S. Rosland

  11. #10
    Member
    Join Date
    Apr 2004
    Posts
    58
    Member #
    5497
    My way worked, guys.

    if(!mysql_fetch_row($check))
    {
    die("...");
    }

    mysql_fetch_row() returns a FALSE value because it $check's result set is empty.

    The only problem I had today was thinking that if the SELECT condition pointed to a row that didn't exist it would generate an error.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:27 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com