问题描述
如何将这些多个查询合并为一个(我可以吗?)
How to join those multiple queries into one (can I?)
$query = "DELETE FROM aktywne_kody WHERE kodsms ='$kodSMSgracza' AND typkodu ='$id'";
mysql_query($query) or die(mysql_error());
$query = "INSERT INTO uzyte_kody (gracz, kodsms, typkodu) VALUES ('$nickGracza', '$kodSMSgracza', '$id')";
mysql_query($query) or die("Błąd MySQL X04");
$query = "INSERT INTO do_odebrania (gracz, itemDATA, itemQTY) VALUES ('$nickGracza', '$itemDATA', '$itemQTY')";
mysql_query($query) or die("Błąd MySQL X05");
顺便说一句,如果我在所有查询完成后执行 mysql_close($db) 会更好吗?
By the way is it better if I do mysql_close($db) after all queries are done?
推荐答案
将 65536
作为第 5 个参数传递给 mysql_connect
.
Pass 65536
to mysql_connect
as 5th parameter.
示例:
$conn = mysql_connect('localhost','username','password', true, 65536 /* here! */)
or die("cannot connect");
mysql_select_db('database_name') or die("cannot use database");
mysql_query("
INSERT INTO table1 (field1,field2) VALUES(1,2);
INSERT INTO table2 (field3,field4,field5) VALUES(3,4,5);
DELETE FROM table3 WHERE field6 = 6;
UPDATE table4 SET field7 = 7 WHERE field8 = 8;
INSERT INTO table5
SELECT t6.field11, t6.field12, t7.field13
FROM table6 t6
INNER JOIN table7 t7 ON t7.field9 = t6.field10;
-- etc
");
当您使用 mysql_fetch_* 或 mysql_num_rows 或 mysql_affected_rows 时,只有第一条语句有效.
When you are working with mysql_fetch_* or mysql_num_rows, or mysql_affected_rows, only the first statement is valid.
比如下面的代码,第一条语句是INSERT,不能执行mysql_num_rows和mysql_fetch_*.用mysql_affected_rows返回插入的行数就可以了.
For example, the following codes, the first statement is INSERT, you cannot execute mysql_num_rows and mysql_fetch_*.It is okay to use mysql_affected_rows to return how many rows inserted.
$conn = mysql_connect('localhost','username','password', true, 65536) or die("cannot connect");
mysql_select_db('database_name') or die("cannot use database");
mysql_query("
INSERT INTO table1 (field1,field2) VALUES(1,2);
SELECT * FROM table2;
");
再比如下面的代码,第一条语句是SELECT,不能执行mysql_affected_rows.但是你可以执行mysql_fetch_assoc得到第一条SELECT语句产生的key-value对行,或者你可以执行mysql_num_rows根据第一条SELECT语句得到行数.
Another example, the following codes, the first statement is SELECT, you cannot execute mysql_affected_rows. But you can execute mysql_fetch_assoc to get a key-value pair of row resulted from the first SELECT statement, or you can execute mysql_num_rows to get number of rows based on the first SELECT statement.
$conn = mysql_connect('localhost','username','password', true, 65536) or die("cannot connect");
mysql_select_db('database_name') or die("cannot use database");
mysql_query("
SELECT * FROM table2;
INSERT INTO table1 (field1,field2) VALUES(1,2);
");
这篇关于使用 PHP 在一个语句中执行多个 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!