Multiple querying

If I want to write a select statement and then follow up with an insert statement, must the 2nd $sql=‘’; be different from the first sql variable? I am not infront of the computer now but let’s say i ended the first query with… mysqli_query ( $conn, $sql)… can i use the same $sql for the insert or would there be some confusion?

If query 2 depends on the results of query 1 you might want to consider wrapping them both up in a transaction, so that if 1 query in a transaction fails, the changes don’t get committed to the database.

Once the first query has been executed, I can’t see that it matters what you do with the string variable that held the query. Obviously you can’t re-use the results object until you’ve finished with it.

I also can’t see why you would want to. IMO terrible practice.

You can re-use it, but I would recommend against that, as re-using variables makes your code harder to read, because you have to read carefully to see what value the variable holds at every point in time. When you don’t re-use variables you don’t have that problem.

Instead I would suggest unique, clear names for variables like $uniqueMemberCheckSql and $signUpMemberSql, something like that.

Of course, you don’t have to use variables for your queries

mysqli_query($conn, 'SELECT foo FROM bar');

is perfectly fine, and even easier to read than using variables imho.

Thanks for the suggestions

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.