How can I do one single query with both an INSERT and SELECT in it and then read the results of selection?
I want to insert and then select something, all must be done in one query…
I know it accept multiple commands in single query…But I want to read results of query and I cannot read results. I’m doing this:
$results=mysql_query("
INSERT INTO table1 (field1,field2) VALUES('aaa','bbbb') ON DUPLICATE KEY UPDATE `field1` = 'cccc', `field2`='dddddd';
SELECT field3 Form Table3 WHERE field3='eeeee';
",$connection);
while ($rows = mysql_fetch_array($results, MYSQL_NUM))
echo $rows[0];
It is possible to send multiple statements in PHP if you are using the
mysqliextension, which is a good idea to use instead of the older mysql extension for a lot of reasons. Here is a modified example from the multiple statements section of the documentation, based on your question:Notice that the documentation does dedicate airtime to security risks of multiple statements, which everyone is pointing out. The other reason, of course, that it’s not always a great idea is if you want the second statement to be affected by the first statement.