Struck in MySQL query results

Discussion in 'MySQL' started by apsam29, Dec 23, 2008.

  1. #1
    I have been using MySQL as backend to my PHP codings.
    Since I found a struck in fetching query results.
    If Im try to pass a query from function(PHP) like "SELECT count(col1) FROM mytable WHERE col1>55;" means, the possible results are:
    1) Error - if query is wrong
    2) 0 - if condition not satisfied
    3) count result - if found 1 or more results

    Here My problem is, if the result may be on 1 or 2 means how can I check its ERROR or 0 from my code?
    :confused:
     
    apsam29, Dec 23, 2008 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    mysql_error() will return you the error if any. This is how you can fetch the results.
     
    mwasif, Dec 24, 2008 IP
  3. planemaniac

    planemaniac Peon

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Say this is your query string:

    
    $query = "SELECT count(col1) FROM myTable WHERE col1 >55"; // This is the query that will be executed on the database
    $execute = mysql_query($query) or die (mysql_error()); // The mysql_query() function executes the query specified, but if it does not work (or die), then display the error MySQL gives. Simple as.
    
    // To find out of the number returned was 0, do this (I think)
    
    $row = mysql_fetch_array($execute);
    
    $num_returned = $row[0];
    
    if($num_returned == 0) {
    echo "Statement returned 0;
    }
    
    
    PHP:
     
    planemaniac, Dec 24, 2008 IP
  4. apsam29

    apsam29 Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Almost like this one, but Im using these statements inside a function getCount("); - so dont want to echo mysql_error() at all.

    If Im getting values from that function it may return ERROR or VALUE . How can identify if 0 and ERROR or differ ?
    If u so interested solve it for me pls ? :confused:
     
    apsam29, Dec 24, 2008 IP
  5. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #5
    Can't say anything unless we see the code of function getCount().
     
    mwasif, Dec 24, 2008 IP