Monday 3 September 2018

The php mysql query does not end after the xampp 1.8.3-4 update

I'm not sure how to even search for this problem because everything was working fine before the update. I'm on a MAC and after updating the OS, XAMPP stopped connecting. So I updated XAMPP also. It's working but my script stops halfway through when it was working before with the older version. Here's what I have:

$test = explode("||", $data);
//Section 1
    mysqli_autocommit($link, FALSE);
    mysqli_query($link, "INSERT INTO x...");
    if (mysqli_commit($link)) {
        $success = mysqli_query($link, "SELECT * FROM x...");
        $check = mysqli_num_rows($success);
        if ($check > 0) {
            $c = array();
            while ($row = mysqli_fetch_assoc($success)) {
                $c = $row;
            }
            echo json_encode($c);
        }
    }
//Section 2
    foreach ($test as $item) {
        if ($item[0] == "#") {
            $query = mysqli_query($link, "INSERT INTO y...");
            if ($query) {
                $q = mysqli_query($link, "SELECT blah FROM z WHERE blah='$item'");
                $check = mysqli_num_rows($q);
                if ($check > 0) {
                    $s = mysqli_query($link, "UPDATE z...");
                }else{
                    $s = mysqli_query($link, "INSERT INTO z...");
            }
        }
    }

So when I run this, section one gets inserted, the echo works and then everything stops. If I comment out section 1 and just run section 2, section 2 works and gets inserted. Is there a reason why it stops halfway???
PS... I know my code isn't perfect, I'll switch to PDO later. Just trying to get the functionality to work for now.
Another thing I tried doing...running the full script, I added this check:
foreach ($test as $item) {
        if ($item[0] == "#") {
            $query = mysqli_query($link, "INSERT INTO y...");
            $q = mysqli_query($link, "SELECT * FROM y");
            $c = mysqli_num_rows($q);
            if ($c > 0) {
                echo "more then 0";
            }
...

And it returns that there is more then 0 entries while my table is still empty...

AUTO COMMIT

so if you have 10 different queries it will be committed 10 times within the query. if you are asking what's problem with this then take a look at this query:
1. UPDATE customer SET savings_account = savings_account - 200 WHERE...
2. UPDATE customer SET savings_account = savings_account + 200 WHERE...

so what if the second query fails? then 200 cash will still be deducted right? that's where setting up the AUTO COMMIT to false enters and the use of transactions(if 1 query fails, the transaction will be invalid and will not be committed to the db).
try{
    $test = explode("||", $data);
    //Section 1
        mysqli_autocommit($link, FALSE);
        mysqli_query($link, "START TRANSACTION"); //<--- start Transaction
        mysqli_query($link, "INSERT INTO x...");
        if (mysqli_commit($link)) {
            $success = mysqli_query($link, "SELECT * FROM x...");
            $check = mysqli_num_rows($success);
            if ($check > 0) {
                $c = array();
                while ($row = mysqli_fetch_assoc($success)) {
                    $c = $row;
                }
                echo json_encode($c);
            }
        }
    //Section 2
        foreach ($test as $item) {
            if ($item[0] == "#") {
                $query = mysqli_query($link, "INSERT INTO y...");
                if ($query) {
                    $q = mysqli_query($link, "SELECT blah FROM z WHERE blah='$item'");
                    $check = mysqli_num_rows($q);
                    if ($check > 0) {
                        $s = mysqli_query($link, "UPDATE z...");
                    }else{
                        $s = mysqli_query($link, "INSERT INTO z...");
                }
            }
        }
        mysqli_query($link, "COMMIT"); //<-- if all queries has been successfully excecuted it will all be saved in the db
}catch($e Exception){
    mysqli_query($link, "ROLLBACK"); //<-- rollback queries.
    throw new Exception("ERROR MESSAGE");
}

0 comments:

Post a Comment