[PHP] Bizarre SQl, if issue....

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Re: [PHP] Bizarre SQl, if issue....

    Did you try to print the content of the variable?
    What does it contain?



    On Tue, 29 Jul 2003 13:29:10 -0400
    "Mike At Spy" <spycobalt@spyproductions.com> wrote:
    >
    > Hey, I've got a weird issue. I have some code, an MySQL statement, that
    > returns an empty set, like this:
    >
    > $person = mysql_query("Some Statement Here");
    >
    > So, then I have an if/else statement:
    >
    > if (!$person || $person == 0 || $person == '' || $person == '0') { $result =
    > 1; }
    > else { $result = 4; }
    >
    > If the SQL statement returns an empty set, shouldn't $result be equal to 1?
    > Instead, I'm getting $result = 4.
    >
    > Hence all of the 'OR' conditions. I would have have thought !$person would
    > be enough.
    >
    > Thanks for any input! :)
    >
    > -Mike
    >
    >
    >
    >
    > --
    > PHP General Mailing List ([url]http://www.php.net/[/url])
    > To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    >
    >

    --
    Lucas Lain
    [email]lainl@aconectarse.com[/email]
    # /Scripting/Manager (??) && /Obscure/Commander
    Lucas Lain Guest

  2. Similar Questions and Discussions

    1. Fonts are there, then they're not. Bizarre.
      I am using InDesign CS on a pc. I use a limited number of fonts to layout our monthly newsletter. I installed the fonts I needed and they were...
    2. 5.80, OS X, bizarre FTP weirdness
      I am running Mac OS X 10.2.7 and recently upgraded from 5.6.1 to 5.8.0 following the guidance at...
    3. Bizarre SQl, if issue....
      Hey, I've got a weird issue. I have some code, an MySQL statement, that returns an empty set, like this: $person = mysql_query("Some Statement...
    4. Bizarre HD Problem
      OS: Win98/Mandrake 9.1 I just installed a new 80GB Maxtor HD (6Y080L0) and, while my BIOS sees it and it shows up in dmesg as 'hdd', a) It...
    5. Bizarre IE6 CSS error
      Hi all, I have the weirdest thing going on... it's hard even to describe, but bascically, IE6 seems to interpret differently the margins/padding...
  3. #2

    Default RE: [PHP] Bizarre SQl, if issue....

    [snip]
    $person = mysql_query("Some Statement Here");

    if (!$person || $person == 0 || $person == '' || $person == '0') {
    $result =
    1; }
    else { $result = 4; }

    If the SQL statement returns an empty set, shouldn't $result be equal to
    1?
    Instead, I'm getting $result = 4.
    [/snip]

    Actually I think we need to see the query == "Some Statement Here".
    $person in this case is either TRUE or FALSE
    Jay Blanchard Guest

  4. #3

    Default RE: [PHP] Bizarre SQl, if issue....


    Here's the SQL - I tested it via command line and it comes back empty set.

    SELECT ID FROM tblItems WHERE number = $place

    Does that help?

    -Mike


    > -----Original Message-----
    > From: Jay Blanchard [mailto:jay.blanchard@niicommunications.com]
    > Sent: Tuesday, July 29, 2003 1:31 PM
    > To: Mike At Spy; [email]php-general@lists.php.net[/email]
    > Subject: RE: [PHP] Bizarre SQl, if issue....
    >
    >
    > [snip]
    > $person = mysql_query("Some Statement Here");
    >
    > if (!$person || $person == 0 || $person == '' || $person == '0') {
    > $result =
    > 1; }
    > else { $result = 4; }
    >
    > If the SQL statement returns an empty set, shouldn't $result be equal to
    > 1?
    > Instead, I'm getting $result = 4.
    > [/snip]
    >
    > Actually I think we need to see the query == "Some Statement Here".
    > $person in this case is either TRUE or FALSE
    >
    >
    Mike At Spy Guest

  5. #4

    Default RE: [PHP] Bizarre SQl, if issue....

    [snip]
    Here's the SQL - I tested it via command line and it comes back empty
    set.

    SELECT ID FROM tblItems WHERE number = $place
    >
    > [snip]
    > $person = mysql_query("Some Statement Here");
    >
    > if (!$person || $person == 0 || $person == '' || $person == '0') {
    > $result =
    > 1; }
    > else { $result = 4; }
    >
    > If the SQL statement returns an empty set, shouldn't $result be equal
    to
    > 1?
    > Instead, I'm getting $result = 4.
    > [/snip]
    >
    > Actually I think we need to see the query == "Some Statement Here".
    > $person in this case is either TRUE or FALSE
    [snip]

    So ...
    $person = mysql_query("SELECT ID FROM tblItems WHERE number = $place");
    $person IS TRUE because query executed (the OR statements do not count
    as $person can only be TRUE or FALSE, so you can drop the OR's);

    if(!$person){ // tests to see if person IS FALSE
    $result = 1;
    } else {
    $result = 4;
    }

    $result will be 4

    If you do this;
    if($person){ // tests to see if person IS TRUE
    $result = 1;
    } else {
    $result = 4;
    }

    $result would be 1

    Now, if you used mysql_num_rows($person)

    $thecount = mysql_num_rows($person);

    if($thecount == 0){ // tests for zero
    $result = 1;
    } else {
    $result = 4;
    }

    $result will be 1

    if(!$thecount == 0){ // tests for other than zero
    $result = 1;
    } else {
    $result = 4;
    }

    $result will be 4

    HTH
    Jay Blanchard Guest

  6. #5

    Default RE: [PHP] Bizarre SQl, if issue....

    [snip]

    ....lots of stuff...

    [/snip]

    What I forgot to say is that basically you were testing for query
    execution, not the number of rows returned from the database. If you
    want to test the number of rows or some other result from the query you
    have to go further.

    HTH!
    Jay Blanchard Guest

  7. #6

    Default RE: [PHP] Bizarre SQl, if issue....

    > -----Original Message-----
    > From: Mike At Spy [mailto:spycobalt@spyproductions.com]
    > Sent: 29 July 2003 18:37
    > >
    > > $person = mysql_query("Some Statement Here");
    Please go back and read the manual page for mysql_query again ([url]http://www.php.net/mysql_query[/url]).

    The above returns FALSE if the query is invalid, or a resource_id you can use to fetch the result (or information about it). So most likely $person is now a MySQL resource id that doesn't match any of the conditions in your if statement, and also (more importantly) doesn't tell you whether you have an empty set or not. I'd suggest mysql_num_rows() for that.

    Cheers!

    Mike

    ---------------------------------------------------------------------
    Mike Ford, Electronic Information Services Adviser,
    Learning Support Services, Learning & Information Services,
    JG125, James Graham Building, Leeds Metropolitan University,
    Beckett Park, LEEDS, LS6 3QS, United Kingdom
    Email: [email]m.ford@lmu.ac.uk[/email]
    Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
    Mike Ford Guest

  8. #7

    Default RE: [PHP] Bizarre SQl, if issue....

    [snip]
    I know. I first tested for !$person but got the TRUE result when I
    should
    have received FALSE (empty set).

    Why would that be?

    I added '0' and such just to see if I wasn't thinking straight..... :\

    -Mike
    [/snip]

    If the query executes it will return TRUE for $person, so

    if(!$person) would read IF THE QUERY IS NOT TRUE

    if($person) would read IF THE QUERY IS TRUE

    Jay Blanchard Guest

  9. #8

    Default RE: [PHP] Bizarre SQl, if issue....

    [snip]
    Off hand, I would agree. And that is what I would expect.

    However, the *exact* same setup on a different server gives me a result
    of
    'FALSE' properly. The other server has php 4.3.3 on it - the one giving
    the
    improper response has php 4.1.2.

    Could this be the issue?
    [/snip]

    It sounds unlikely. The point is, what are you needing to test for? Do
    you need to know that the query executed or do you need to test the
    results of the query for a particular condition?

    Jay Blanchard Guest

  10. #9

    Default RE: [PHP] Bizarre SQl, if issue....


    Off hand, I would agree. And that is what I would expect.

    However, the *exact* same setup on a different server gives me a result of
    'FALSE' properly. The other server has php 4.3.3 on it - the one giving the
    improper response has php 4.1.2.

    Could this be the issue?

    -Mike


    > -----Original Message-----
    > From: Ford, Mike [LSS] [mailto:M.Ford@lmu.ac.uk]
    > Sent: Tuesday, July 29, 2003 1:47 PM
    > To: 'Mike At Spy'; [email]php-general@lists.php.net[/email]
    > Subject: RE: [PHP] Bizarre SQl, if issue....
    >
    >
    > > -----Original Message-----
    > > From: Mike At Spy [mailto:spycobalt@spyproductions.com]
    > > Sent: 29 July 2003 18:37
    >
    > > >
    > > > $person = mysql_query("Some Statement Here");
    >
    > Please go back and read the manual page for mysql_query again
    > (http://www.php.net/mysql_query).
    >
    > The above returns FALSE if the query is invalid, or a resource_id
    > you can use to fetch the result (or information about it). So
    > most likely $person is now a MySQL resource id that doesn't match
    > any of the conditions in your if statement, and also (more
    > importantly) doesn't tell you whether you have an empty set or
    > not. I'd suggest mysql_num_rows() for that.
    >
    > Cheers!
    >
    > Mike
    >
    > ---------------------------------------------------------------------
    > Mike Ford, Electronic Information Services Adviser,
    > Learning Support Services, Learning & Information Services,
    > JG125, James Graham Building, Leeds Metropolitan University,
    > Beckett Park, LEEDS, LS6 3QS, United Kingdom
    > Email: m.ford@lmu.ac.uk
    > Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
    >
    >

    Mike At Spy Guest

  11. #10

    Default RE: [PHP] Bizarre SQl, if issue....

    > -----Original Message-----
    > From: Mike At Spy [mailto:spycobalt@spyproductions.com]
    > Sent: 29 July 2003 19:01
    >
    > Off hand, I would agree. And that is what I would expect.
    >
    > However, the *exact* same setup on a different server gives
    > me a result of
    > 'FALSE' properly. The other server has php 4.3.3 on it - the
    > one giving the
    > improper response has php 4.1.2.
    Hang on -- it's not clear to me which server is giving which response.

    Just to be clear, the proper response is to deliver a resource id if the query is valid, or FALSE if not. Could it be that one server is consistently giving you a resource ID of 0, and the other isn't? (I'm not even sure if a resource ID of zero is equivalent to a real zero, or what the result of doing an if ($resource_id) on it would be, but this might be a straw to clutch at!). To see *exactly* what's being returned into $person from your mysql_query(), try doing var_dump($person).

    Cheers!

    Mike

    ---------------------------------------------------------------------
    Mike Ford, Electronic Information Services Adviser,
    Learning Support Services, Learning & Information Services,
    JG125, James Graham Building, Leeds Metropolitan University,
    Beckett Park, LEEDS, LS6 3QS, United Kingdom
    Email: [email]m.ford@lmu.ac.uk[/email]
    Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
    Mike Ford Guest

  12. #11

    Default RE: [PHP] Bizarre SQl, if issue....

    > [snip]
    > Off hand, I would agree. And that is what I would expect.
    >
    > However, the *exact* same setup on a different server gives me a result
    > of
    > 'FALSE' properly. The other server has php 4.3.3 on it - the one giving
    > the
    > improper response has php 4.1.2.



    $which_person = mysql_query("SELECT ID FROM tblItems WHERE number =
    $place");

    $vthere = mysql_num_rows($which_person);

    if ($vthere == '0') { True } else { False }


    What should this return? True or False?

    -Mike



    Mike At Spy Guest

  13. #12

    Default RE: [PHP] Bizarre SQl, if issue....

    [snip]
    $which_person = mysql_query("SELECT ID FROM tblItems WHERE number =
    $place");

    $vthere = mysql_num_rows($which_person);

    if ($vthere == '0') { True } else { False }


    What should this return? True or False?
    [/snip]

    If there is one or more rows returned by the query False, if zero rows
    are returned then True.
    Jay Blanchard Guest

  14. #13

    Default RE: [PHP] Bizarre SQl, if issue....


    I did the var_dump and got:

    bool(false)

    While $vthere (from my previous post) returns 0 (zero) still. :\

    But proceeds as if 0 were a false statement when asked if $vthere equals
    zero. :\

    -Mike


    > -----Original Message-----
    > From: Ford, Mike [LSS] [mailto:M.Ford@lmu.ac.uk]
    > Sent: Tuesday, July 29, 2003 2:07 PM
    > To: 'Mike At Spy'; Ford, Mike [LSS]; [email]php-general@lists.php.net[/email]
    > Subject: RE: [PHP] Bizarre SQl, if issue....
    >
    >
    > > -----Original Message-----
    > > From: Mike At Spy [mailto:spycobalt@spyproductions.com]
    > > Sent: 29 July 2003 19:01
    > >
    > > Off hand, I would agree. And that is what I would expect.
    > >
    > > However, the *exact* same setup on a different server gives
    > > me a result of
    > > 'FALSE' properly. The other server has php 4.3.3 on it - the
    > > one giving the
    > > improper response has php 4.1.2.
    >
    > Hang on -- it's not clear to me which server is giving which response.
    >
    > Just to be clear, the proper response is to deliver a resource id
    > if the query is valid, or FALSE if not. Could it be that one
    > server is consistently giving you a resource ID of 0, and the
    > other isn't? (I'm not even sure if a resource ID of zero is
    > equivalent to a real zero, or what the result of doing an if
    > ($resource_id) on it would be, but this might be a straw to
    > clutch at!). To see *exactly* what's being returned into $person
    > from your mysql_query(), try doing var_dump($person).
    >
    > Cheers!
    >
    > Mike
    >
    > ---------------------------------------------------------------------
    > Mike Ford, Electronic Information Services Adviser,
    > Learning Support Services, Learning & Information Services,
    > JG125, James Graham Building, Leeds Metropolitan University,
    > Beckett Park, LEEDS, LS6 3QS, United Kingdom
    > Email: m.ford@lmu.ac.uk
    > Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    >

    Mike At Spy Guest

  15. #14

    Default RE: [PHP] Bizarre SQl, if issue....

    [snip]
    I did the var_dump and got:

    bool(false)

    While $vthere (from my previous post) returns 0 (zero) still. :\

    But proceeds as if 0 were a false statement when asked if $vthere equals
    zero. :\

    -Mike
    [/snip]

    Is the query executing properly? Try this ...

    $sql = "SELECT ID from tblItems WHERE number = '" . $place . "' ");
    if(!($person = mysql_query($sql, $connect_variable))){
    print(mysql_error() . "\n");
    exit();
    }

    It is a verbose test
    Jay Blanchard Guest

  16. #15

    Default RE: [PHP] Bizarre SQl, if issue....


    Nothin' wrong with the SQL statement - it executes properly when I enter
    values that actually pull something from a table. When I enter values that
    are not present in the table, I go on to have the other issue of it looking
    like it is false, though it is not. :\

    -Mike


    > -----Original Message-----
    > From: Jay Blanchard [mailto:jay.blanchard@niicommunications.com]
    > Sent: Tuesday, July 29, 2003 2:24 PM
    > To: Mike At Spy; Ford, Mike [LSS]; [email]php-general@lists.php.net[/email]
    > Subject: RE: [PHP] Bizarre SQl, if issue....
    >
    >
    > [snip]
    > I did the var_dump and got:
    >
    > bool(false)
    >
    > While $vthere (from my previous post) returns 0 (zero) still. :\
    >
    > But proceeds as if 0 were a false statement when asked if $vthere equals
    > zero. :\
    >
    > -Mike
    > [/snip]
    >
    > Is the query executing properly? Try this ...
    >
    > $sql = "SELECT ID from tblItems WHERE number = '" . $place . "' ");
    > if(!($person = mysql_query($sql, $connect_variable))){
    > print(mysql_error() . "\n");
    > exit();
    > }
    >
    > It is a verbose test
    >
    >

    Mike At Spy Guest

  17. #16

    Default RE: [PHP] Bizarre SQl, if issue....

    [snip]
    Nothin' wrong with the SQL statement - it executes properly when I enter
    values that actually pull something from a table. When I enter values
    that
    are not present in the table, I go on to have the other issue of it
    looking
    like it is false, though it is not. :\
    [/snip]

    But you are testing the query from the command line, I want to test it
    from within the confines of the PHP script. Humor me?
    Jay Blanchard Guest

  18. #17

    Default Re: [PHP] Bizarre SQl, if issue....

    Hello,

    This is a reply to an e-mail that you wrote on Tue, 29 Jul 2003 at
    18:57, lines prefixed by '>' were originally written by you.
    > Off hand, I would agree. And that is what I would expect.
    > However, the *exact* same setup on a different server gives me a
    > result of
    > 'FALSE' properly. The other server has php 4.3.3 on it - the one
    > giving the
    > improper response has php 4.1.2.
    > Could this be the issue?
    > -Mike
    mysql_query() returns FALSE if the query fails and a resource ID if
    it suceeds in both of those versions. The best way to see if it
    failed or not is...

    $queryid = mysql_query($sql);
    if($queryid !== FALSE){
    // this will be evaluated if mysql_wuery did not return FALSE
    // and it is type-sensitive so (int)0 does not count as FALSE.
    }

    --
    phpmachine :: The quick and easy to use service providing you with
    professionally developed PHP scripts :: [url]http://www.phpmachine.com/[/url]

    Professional Web Development by David Nicholson
    [url]http://www.djnicholson.com/[/url]

    QuizSender.com - How well do your friends actually know you?
    [url]http://www.quizsender.com/[/url]
    (developed entirely in PHP)
    David Nicholson Guest

  19. #18

    Default RE: [PHP] Bizarre SQl, if issue....

    [snip]
    > I still want to know what you need to do, test for query execution or
    > value? It is pretty cut and dried after that.
    Value. If there is nothing found in the table that matches, do one
    thing.
    If there is, do another. :)
    [/snip]

    Then you must test for something returned, not whether or not the query
    executed. Use mysql_num_rows($person);
    Jay Blanchard Guest

  20. #19

    Default RE: [PHP] Bizarre SQl, if issue....


    > -----Original Message-----
    > From: Jay Blanchard [mailto:jay.blanchard@niicommunications.com]
    > Sent: Tuesday, July 29, 2003 2:41 PM
    > To: Mike At Spy; [email]php-general@lists.php.net[/email]
    > Subject: RE: [PHP] Bizarre SQl, if issue....
    >
    >
    > [snip]
    > > I still want to know what you need to do, test for query execution or
    > > value? It is pretty cut and dried after that.
    >
    > Value. If there is nothing found in the table that matches, do one
    > thing.
    > If there is, do another. :)
    > [/snip]
    >
    > Then you must test for something returned, not whether or not the query
    > executed. Use mysql_num_rows($person);
    I did! Look at this again:

    $which_person = mysql_query("SELECT ID FROM tblItems WHERE number =
    $place");

    $vthere = mysql_num_rows($which_person);

    if ($vthere == '0') { True } else { False }

    Yet it still returns as if this is FALSE (i.e. it executes the else {}. :\

    :\

    -Mike



    Mike At Spy Guest

  21. #20

    Default Re: [PHP] Bizarre SQl, if issue....

    Hello,

    This is a reply to an e-mail that you wrote on Tue, 29 Jul 2003 at
    19:46, lines prefixed by '>' were originally written by you.
    > I did! Look at this again:
    > $which_person = mysql_query("SELECT ID FROM tblItems WHERE
    number =
    > $place");
    > $vthere = mysql_num_rows($which_person);
    > if ($vthere == '0') { True } else { False }
    > Yet it still returns as if this is FALSE (i.e. it executes the
    else
    > {}. :
    As long as your query is returning 0 rows that code should work ok,
    but it is incorrect for correctness,
    if ($vthere == '0')
    should be
    if ($vthere == 0)
    As $vthere will contain an integer, not a string, but as you are
    using the == comparison operator instead of === PHP should
    convert them both to the same type before comparing.

    Can we see the snippet of the actual code that you are using?

    David.

    --
    phpmachine :: The quick and easy to use service providing you with
    professionally developed PHP scripts :: [url]http://www.phpmachine.com/[/url]

    Professional Web Development by David Nicholson
    [url]http://www.djnicholson.com/[/url]

    QuizSender.com - How well do your friends actually know you?
    [url]http://www.quizsender.com/[/url]
    (developed entirely in PHP)
    David Nicholson Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139