0

I have all my script working good except these two functions which are meant to simply query a database. I have checked all variables. Tested both functions flow but no luck. Both queries are returning false. Here is the relevant two functions:

function check_attempts($uid) {
    global $conn;

    $stmt = mysqli_query($conn, "SELECT attempted, time FROM user_attempts WHERE uid = '$uid'");

    if(mysqli_num_rows($stmt) >= 5) {
        $stmt_2 = mysqli_query("UPDATE users SET locked = '1'");
        if($stmt_2) {
            return false;
        }
    }
    else {
        return true;
    }
}
function update_attempt($uid) {
    global $conn;
    $now = time();

    $stmt = mysqli_query($conn, "INSERT INTO user_attempts(attempted, time, uid) VALUES ('1', '$now', '$uid')");
    if($stmt) {
        return false;
    }
    else {
        echo "Error in Query";
    }
}

I have queried the database manually and both of them worked nicely. I don't think that I am doing anything wrong.

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
  • What does `mysqli_error()` tell you? – andrewsi Sep 01 '13 at 16:44
  • @andrewsi The first query simply says: `Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given` which means the query failed and second one as given prints out `Error in Query`. – Muhammad Talha Akbar Sep 01 '13 at 16:45
  • @MuhammadTalhaAkbar: When you create `$conn`, also add this line of code: `mysqli_report(MYSQLI_REPORT_STRICT);` - this ensures that you are more tight with your errors. – hakre Sep 01 '13 at 16:46
  • 1
    "I have queried the database manually and both of them worked nicely." - if they are both working nicely, there is only 1 possibility: something is wrong with connection – user4035 Sep 01 '13 at 16:47
  • Try adding `echo mysqli_error($conn)` to your code. Add it after the query is run, and before you try and use the return value – andrewsi Sep 01 '13 at 16:47
  • I'd do a sanity check, first copy and paste your query into MySQL Workbench, and replace all variables with static realistic values. If the query does not work, it's the structure of your query. If the query works, it's the variables being passed in ... perhaps escaping improperly or a value is not being assigned correctly. – half-fast Sep 01 '13 at 16:48
  • 1
    Not the problem, but you could use `VALUES ('1', NOW(), '$uid')`, NOW() is a MySQL function and it will put the current date and time if your field is a DATETIME or TIME or DATE field. – Prix Sep 01 '13 at 16:48
  • @andrewsi I have used it and it prints out: `Commands out of sync; you can't run this command now` – Muhammad Talha Akbar Sep 01 '13 at 16:51
  • @Prix Let me see but I think it is not the issue. – Muhammad Talha Akbar Sep 01 '13 at 16:52
  • @MuhammadTalhaAkbar - then you already have a connection open with an active query on it. – andrewsi Sep 01 '13 at 16:53
  • Its not its just a better way to update the time as I have early mentioned. Here is what your issue is per your error http://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now – Prix Sep 01 '13 at 16:53
  • @andrewsi Yeah I have one query before these two functions are called. – Muhammad Talha Akbar Sep 01 '13 at 16:55
  • @MuhammadTalhaAkbar - and that query needs to be closed before you call those functions. Or you can run them using a separate database handle. But that's what's causing your errors. – andrewsi Sep 01 '13 at 16:57
  • @andrewsi Let me see! It is not a good idea to use another connection. – Muhammad Talha Akbar Sep 01 '13 at 16:59
  • @MuhammadTalhaAkbar - It's not best practice, but without seeing the code where you're calling these functions.... – andrewsi Sep 01 '13 at 17:00

0 Answers0