0

I was wondering how would go about executing two different queries like this:

    if ($uresult->num_rows >0) {


 while($urow = $uresult->fetch_assoc()) {


 $rresult = mysqli_query($con,"SELECT * FROM allid WHERE postid='$oldid' AND spaceid='$newid'");
 $rresult = mysqli_query($con,"DELETE FROM allid WHERE postid AND spaceid IS NULL");
 $lrow = mysqli_fetch_assoc($rresult);
 $tem = $lrow['postid'];

 $ujson = json_encode($tem);
 echo $ujson;
 }

} else {
}

I know mysqli_fetch can't hold no more than one query and there are similar questions, but I can't seem to understand the answers from the other questions. If there is a question that solves this question, I apologize and will delete this one.

1
  • 1
    you use $rresult for both queries, just change one .. solved (you dont even need to assign the delete one to a var Commented May 28, 2018 at 23:13

2 Answers 2

1

all other things being equal, simple dont overwrite the $rresult from select with delete:

if ($uresult->num_rows >0) {

    while($urow = $uresult->fetch_assoc()) {
        $rresult = mysqli_query($con,"SELECT * 
                                      FROM allid 
                                      WHERE postid='$oldid' 
                                      AND spaceid='$newid'");
        mysqli_query($con,"DELETE FROM allid 
                           WHERE postid AND spaceid IS NULL");
        $lrow = mysqli_fetch_assoc($rresult);
        $tem = $lrow['postid'];

        $ujson = json_encode($tem);
        echo $ujson;
    }

} else {
}
Sign up to request clarification or add additional context in comments.

16 Comments

Ok, I deleted the variable and the second query is not deleting the null from the results. Does placement matter? @smith
@LaneyWilliams did you mean to write WHERE postid IS NULL AND spaceid IS NULL ?
I believe so and I just tested that. Here is what I am getting: "10"nullnullnullnullnullnull"202"
you are deleting after you select, did you mean to do it the other way around?
Oh no. I just want to remove those null from the results above. I looked up other answers and they said to use IS NULL.
|
0

If you have multiple queries that either rely on a result of a previous query or need to be run in order and each successfully run, you could use transactions. With transactions if one of the queries fails, you can roll back the transaction.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.