[英]how to execute multiple update queries in PHP using MySQL?
I have to execute multiple update queries in single query using mysql_query() funciton of PHP. 我必须使用PHP的mysql_query()函数在单个查询中执行多个更新查询。 I append multiple update statements to form single query to pass parameter to mysql_query() function. 我附加多个更新语句以形成单个查询,以将参数传递给mysql_query()函数。 So at the end I am getting one combined query but it is found to be wrong. 因此,最后我得到了一个组合查询,但是发现它是错误的。 When I run that query at that time during execution I am getting error as "Invalid query". 当我在执行过程中当时运行该查询时,出现错误,显示为“无效查询”。 I do not understand what is wrong happening , so please help me in this quetion . 我不了解发生了什么问题,因此请在此问题中帮助我。 I am suffering from it so badly. 我遭受如此严重的痛苦。 I tried my best. 我已经尽力了。 I am giving my code for reference. 我正在提供我的代码以供参考。
Here I am getting values of varibles through querystring 在这里,我通过querystring获取变量的值
my querystirng : localhost/ticker/publish-ticker.php?i=1,2,3&j=3,4
我的querystirng: localhost/ticker/publish-ticker.php?i=1,2,3&j=3,4
if(isset($_GET['i']))
{
$selected_tickers = $_GET['i'];
}
if(isset($_GET['j']))
{
$selected_twitters = $_GET['j'];
}
Here I am forming the query 我在这里形成查询
$sql2 = "";
if(count($sel_ticker_array) > 0 )
{
foreach($sel_ticker_array as $value2)
{
$tid1 = preg_replace('/[^0-9]/','', $value2);
$sql2 .= "update tbl_ticker2 set ticker_flag=0 where id!=$tid1;";
}
foreach($sel_ticker_array as $value)
{
$tid3 = preg_replace('/[^0-9]/','', $value);
$sql2 .= "update tbl_ticker2 set ticker_flag=1,last_used='$todaysdate' where id=$tid3;";
}
}
if(count($sel_twitter_array) > 0)
{
foreach($sel_twitter_array as $value)
{
$tid = preg_replace('/[^0-9]/','', $value);
$sql2 .= "update tbl_ticker2 set twitter_flag=0 where id!=$tid;";
}
foreach($sel_twitter_array as $value)
{
$tid = preg_replace('/[^0-9]/','', $value);
$sql2 .= "update tbl_ticker2 set twitter_flag=1,last_used='$todaysdate' where id=$tid;";
}
}
Here I am executing it but I am getting output as "Invalid query" 在这里,我正在执行它,但输出为“无效查询”
$query3 = mysql_query($sql2);
if(!$query3)
{
die("Invalid Delivery");
}
else
{
echo '<script>';
echo 'alert("Ticker Updated Successfully");';
echo 'location.href="add_ticker.php"';
echo '</script>';
}
Please help me. 请帮我。
You can't do that using the regular mysql-api in PHP. 您无法使用PHP中的常规mysql-api来做到这一点。
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_query()将唯一查询(不支持多个查询)发送到与指定的link_identifier关联的服务器上当前活动的数据库。
it can be done using mysqli and the mysqli_multi_query() function. 可以使用mysqli和mysqli_multi_query()函数来完成。
also read : 还读:
Manual states that you cannot use it for multiple queries. 手册指出您不能将其用于多个查询。
http://php.net/manual/en/function.mysql-query.php http://php.net/manual/zh/function.mysql-query.php
You can use mysqli or pdo for this. 您可以为此使用mysqli或pdo。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.