从PHP运行多个MySQL查询|为什么这不起作用? [英] Run multiple MySQL queries from PHP | Why is this not working?

查看:50
本文介绍了从PHP运行多个MySQL查询|为什么这不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从php文件对某些mysql数据库运行多个查询.但是,它不起作用.谁能指出我要去哪里错了?

I am trying to run multiple queries on some mysql dbs from a php file. However it is not working. Can anyone point out where I am going wrong?

这是php文件的内容:

Here is the contents of the php file:

<?
require_once('/home/xxxxxx/public_html/xxxxxx/dbconnect.php');
$query = "
TRUNCATE TABLE db2.table1;
INSERT INTO db2.table1
SELECT 
       column1, column2, column3, column4
       FROM db1.table1;

TRUNCATE TABLE db2.table2;
INSERT INTO db2.table2
SELECT 
       column1, column2, column3, column4 
       FROM db1.table2;
ANALYZE TABLE db2.table2;
";

$result = @mysql_query($query);    
?>  

在此先感谢您的帮助.

推荐答案

您仅进行一次查询.阅读 mysql_query

You are making only one query. Read docs for mysql_query

mysql_query()发送一个唯一的查询 (不支持多个查询) 到当前处于活动状态的数据库 与 指定的link_identifier.

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

您应该将每个查询拆分为自己的字符串,然后将其一一发送给MySQL.

You should split each query in it's own string and send it one by one to MySQL.

$query1 = "TRUNCATE TABLE db2.table1;";
$query2 = "INSERT INTO db2.table1
SELECT 
       column1, column2, column3, column4
       FROM db1.table1;";

$query3 = "TRUNCATE TABLE db2.table2;";
$query4 = "INSERT INTO db2.table2
SELECT 
       column1, column2, column3, column4 
       FROM db1.table2;";
$query5= "ANALYZE TABLE db2.table2;";

$result1 = @mysql_query($query1);  
$result2 = @mysql_query($query2);  
$result3 = @mysql_query($query3);  
$result4 = @mysql_query($query4);  
$result5 = @mysql_query($query5); 

或使用其他支持多种查询的接口功能.我个人不知道是否有这样的事情.

Or use some other interface function that supports multiple queries. Personally I don't know if there is such a thing.

这篇关于从PHP运行多个MySQL查询|为什么这不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆