如何将这些多个查询合并为一个(可以吗?)
$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),那会更好吗?
传递65536到mysql_connect作为第五个参数。
65536
mysql_connect
例:
$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时,仅第一条语句有效。
例如,以下代码,第一个语句为INSERT,则无法执行mysql_num_rows和mysql_fetch_ *。可以使用mysql_affected_rows返回插入的行数。
$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语句生成的行的键值对,也可以执行mysql_num_rows来获取基于第一个SELECT语句的行数。
$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); ");