#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Mysql query returns null value


    I have code that runs two mysql queries. The second query depends on the result of the first returning true. This works fine, that is to say the value of $result is true and the code moves to the next query. However when the second query runs, which it does and works, it returns a null value for $result.
    In testing the code I added some echo statements. The first just shows the value "RESULT". The second, Records deleted showed a -1 value for the affected rows, when in fact one record was deleted. As I said, the both queries work fine.
    Can someone tell me why the result value for the second query is null and why the number of record affect = -1?

    Thanks,
    Tom

    $query = DeleteTerm($TermID);
    $result = mysql_query($query);
    if ($result)
    {
    $query = DeleteTermFromGroupTermLink($TermID);
    $result = mysql_query($query);
    echo "RESULT " . $rc . "<br/>";
    echo "Records deleted: ", mysql_affected_rows();
    if ($result)
    {
    $lblMessage = "Delete Term Was Successful.";
    }
    else
    {
    $lblErrMessage = "Delete Term Encountered An Error. Please Try Again.";
    }
    }
    else
    {
    $lblErrMessage = "Delete Term Encountered An Error. Please Try Again.";
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    You might want to show the two functions you use, assuming they 'contain' the SQL that is run.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,503
    Rep Power
    594
    1) Please enclose your code in [ PHP ] tags. See the sticky at the top of the forum.
    2) You should not be using the MySQL extensions. Switch to PDO and add some error checking.
    3) The -1 indicates the previous query failed.
    4) If the previous query is really a DELETE then it must return either true or false. You are misinterpreting null for false.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0
    Originally Posted by gw1500se
    1) Please enclose your code in [ PHP ] tags. See the sticky at the top of the forum.
    2) You should not be using the MySQL extensions. Switch to PDO and add some error checking.
    3) The -1 indicates the previous query failed.
    4) If the previous query is really a DELETE then it must return either true or false. You are misinterpreting null for false.
    Ah, but the query did not fail and there is lies the mystery. The record/s did indeed get deleted. That said, I think I found the problem. The table the from which the record was being deleted had two fields, each a key field (linking table). The delete query passed in a value for only one of the two fields, which was my error. When I corrected the query to pass both key fields as parameters I got a good return code = 1. To be honest I'm still not sure why I got the bad return code. I ran the original code in phpMyadmin and it worked fine.
    At this point I can only assume someone up above was tying to let me know what a knucklehead I was not passing in two parameters.

    Thanks for you help,
    Tom

IMN logo majestic logo threadwatch logo seochat tools logo