How to execute PHP mysqli multi query by setting variables - php

I want to execute a multi query in PHP MySQL, and get the result. My queries are like this.
$sql1 = "set #uid:=(select chat_id from chat_notify where online=1 and engaged=0 limit 1);";
$sql1 .= "update chat_notify set engaged=1,emp_id=1 where chat_id=#uid;";
$sql1 .= " select #uid";
$sql = $mysqli->multi_query($sql1);
I want to get the result of the uid in php.

Change your code accordingly
$sql1 = "set #uid:=(select chat_id from chat_notify where online=1 and engaged=0 limit 1)";
$sql2 = "update chat_notify set engaged=1,emp_id=1 where chat_id=#uid";
$sql3 = "select #uid";
$mysqli->query($sql1);
$mysqli->query($sql2);
$res = $mysqli->query($sql3);
and then get your query result usual way

Related

PHP MYSQL Update several records in while loop

I have that simple php code:
$sql= 'SELECT ID, fb_postid, scheduled FROM `posts` WHERE clicks = "" AND fb_postid !="" AND scheduled < NOW() order by ID ASC LIMIT 10;';
$result = mysqli_query($link, $sql);
print_r($result);
while ($row = mysqli_fetch_assoc($result)) {
$clicks=fbcall($fbtoken, $row['fb_postid']);
$update="UPDATE `posts` SET `clicks`='".$clicks."' WHERE id='".$row['ID']."'";
$result = mysqli_query($link, $update);
print("POSTID: " . $row['fb_postid'] . " - Clicks: " . $clicks ."<br>");
};
The MYSQL SELECT gets 10 Lines from DB loops through the line in the While Loop, gets "clicks" from function fbcall and then should update all 10 lines with the values from "clicks" to the db. if i run the code without update i get 10 results printed but if i run with mysqli_update i just get 1 row updated. Anybody any Idea why?
You're using the $result variable to iterate through a list of rows from the first query.
But you're giving $result a new value within the loop, clearing whatever was there.
Just use two different variables, and you'll be fine. Try something like this:
$sql= 'SELECT ID, fb_postid, scheduled FROM `posts` WHERE clicks = "" AND fb_postid !="" AND scheduled < NOW() order by ID ASC LIMIT 10;';
$result = mysqli_query($link, $sql);
print_r($result);
while ($row = mysqli_fetch_assoc($result)) {
$clicks=fbcall($fbtoken, $row['fb_postid']);
$update="UPDATE `posts` SET `clicks`='".$clicks."' WHERE id='".$row['ID']."'";
$result2 = mysqli_query($link, $update);
print("POSTID: " . $row['fb_postid'] . " - Clicks: " . $clicks ."<br>");
};
Fixed it and built with prepared update statement:
$sql= 'SELECT ID, fb_postid, scheduled FROM `posts` WHERE clicks = "" AND fb_postid !="" AND scheduled < NOW() order by ID ASC LIMIT 10;';
$result = mysqli_query($link, $sql);
$qry = "UPDATE `posts` SET `clicks`=? WHERE id=?";
$stmt = mysqli_prepare ($link, $qry);
mysqli_stmt_bind_param ($stmt, 'ss',$clicks, $id);
while ($row = mysqli_fetch_assoc($result)) {
$clicks=fbcall($fbtoken, $row['fb_postid']);
$id=$row['ID'];
mysqli_stmt_execute($stmt);
print("POSTID: " . $row['fb_postid'] . " - Clicks: " . $clicks ."<br>");
};

How to run multiple query on single save button?

When I click on the save button only all_university table updates but the all_colleges table has not been updated. How can I update two tables on one save button?
<?php
if(isset($_POST['save'])) {
$chk = implode(",", $_POST['company_name']);
$sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."'";
$sql = "update all_colleges set placement = '$chk' where college_name = '".$_POST['college_name']."'";
$value = mysqli_multi_query($link,$sql);
if($value == true){
$msg .="<h5 style='color:green'>Successfull</h5>";
} else {
$msg .="<h5 style='color:red'>Error!</h5>";
}
}
?>
It doesn't matter how many queries you have. Just run them all one by one.
Besides, you should be using prepared statements.
<?php
if(isset($_POST['save'])) {
$chk = implode(",", $_POST['company_name']);
$sql = "update all_university set placement = ? where university_name = ?";
$stmt = $link->prepare($sql);
$stmt->bind_param("ss", $chk, $_POST['university_name']);
$stmt->execute();
$sql = "update all_colleges set placement = ? where college_name = ?";
$stmt = $link->prepare($sql);
$stmt->bind_param("ss", $chk, $_POST['college_name']);
$stmt->execute();
$msg .="<h5 style='color:green'>Successfull</h5>";
}
DO NOT use mysqli_multi_query(). It will do you no good and won't work the way you think.
If you want to use the multi-query function, you have to concatenate the two query strings:
$sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."';";
$sql .= "update all_colleges set placement = '$chk' where college_name = '".$_POST['college_name']."'";
And then execute mysqli_multi_query.
Or, as Rory already mentioned, just query twice using the normal mysqli_query function.
But you should really look into prepared statements as you are vulnerable to SQL injection!
Separate both query with semicolon (;)
$sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."';";
$sql = "update all_colleges set placement = '$chk' where college_name = '".$_POST['college_name']."'";
Then, Append Both Query.
$sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."'";
$sql .= "update all_colleges set placement = '$chk' where college_name = '".$_POST['college_name']."'";
Execute it.
$value = mysqli_multi_query($link,$sql);
The mysqli_multi_query() function performs one or more queries against
the database. The queries are separated with a semicolon.

Executing Multiple or Three Queries at Once

I'm currently creating an online reservation system.
How can I execute multiple queries at once?
I'm already using mysqli_muti_query. I have three queries to execute.
<?php
$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";
$sql .= "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye' ";
$sql .= "UPDATE vehicles set status = '$un' WHERE plateno = '$plate' "; ?>
You need to Add semicolon after each statement:
<?php
$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";
$sql .= "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye' ;";
$sql .= "UPDATE vehicles set status = '$un' WHERE plateno = '$plate' ;";
If supported by your database, I'd suggest that you run your queries separately and wrap them into a single transaction.
$conn->begin_transaction();
$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";
$conn->query($sql);
$sql = "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye';";
$conn->query($sql);
$sql = "UPDATE vehicles set status = '$un' WHERE plateno = '$plate';";
$conn->query($sql);
$conn->commit();
I'd also suggest that you use prepared statements with parameters rather than including values directly into your query string.
You can use PHP mysqli_multi_query-function.
$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";
$sql .= "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye' ;";
$sql .= "UPDATE vehicles set status = '$un' WHERE plateno = '$plate' ;";
/* execute multi query */
if ($mysqli->multi_query($query)) {
}

Update multiple rows using for loop and array php

I have this code:
$id1=array($aa1,$aa2,$aa3,$aa4,$aa5,$aa6,$aa7);
$rank1=array($a1,$a4,$a7,$a10,$a13,$a16,$a19);
require_once("connection.php");
for($i=0;$i<7;$i++){
$sql = "update live_tracking set swim_rank = '"$rank1[$i]."' where id = '".$id1[$i]."'";
}
I want to update multiple rows of my mysql database but using the code above only the first index is being updated. Any suggestions?
You can execute one query only by using this :
$sql = "update live_tracking set swim_rank = '".$rank1[$i]."' WHERE id IN (";
for($i=0;$i<7;$i++){
$sql .= $id1[$i].",";
}
$sql .= ")";
$query = mysql_query($sql) or die("error : ".mysql_error());
if($query){
echo "success";
}
Execute the query within the loop itself.
for($i=0;$i<7;$i++)
{
mysql_query("update live_tracking set swim_rank = '".$rank1[$i]."' where id = '".$id1[$i]."'");
}
I can see an error in the sql
$sql = "update live_tracking set swim_rank = '"$rank1[$i]."' where id = '".$id1[$i]."'";
The above should be
$sql = "update live_tracking set swim_rank = '".$rank1[$i]."' where id = '".$id1[$i]."'";
You were missing . in '".$rank1[$i]."'
It seems that you are on right track. but you need to execute query in the loop. like this:
$id1=array($aa1,$aa2,$aa3,$aa4,$aa5,$aa6,$aa7);
$rank1=array($a1,$a4,$a7,$a10,$a13,$a16,$a19);
require_once("connection.php");
for($i=0;$i<7;$i++){
$sql = "update live_tracking set swim_rank = '".$rank1[$i]."' where id = '".$id1[$i]."'";
if(!mysql_query($sql)){
echo "not updated".$id1[$i]; exit();
}
}

Multiple DELETES in one query are not working on PHP page, but working in phpMyAdmin

I want to run one query with multiple statements to delete records from my database all at once. I construct a mysql query like so:
$query = "DELETE FROM myTable1 WHERE indexID = '".$myVar."' LIMIT 1; ";
$query .= "DELETE FROM myTable2 WHERE indexID = '".$myVar."' LIMIT 1; ";
$query .= "DELETE FROM myTable3 WHERE indexID = '".$myVar."' LIMIT 6; ";
Then, I call my query something like this:
if(!$result = mysqli_query($db, $query)) {
echo "error with $query";
}
Am I doing something incorrect? The query that gets printed out, when copied and run inside phpMyAdmin works just fine.
I think you should create a array of query and then you execute in loop
like
$query = array["DELETE FROM myTable1 WHERE indexID = '".$myVar."' LIMIT 1",
"DELETE FROM myTable2 WHERE indexID = '".$myVar."' LIMIT 1",
DELETE FROM myTable3 WHERE indexID = '".$myVar."' LIMIT 6"];
and then you you execute in loop
for($i=0; $i<sizeof($query);$i++)
{
$result[$i]=mysql_query($con,$query[$i]);
}
The answer to this question was to use the built-in php function:
mysqli_multi_query($db, $query)
It's not limited to just DELETE queries, but any multi-statement queries. But since DELETE doesn't return any values I am interested in like a SELECT would, we don't need to worry so much about the result set of the query.
In case we are concerned with the results set then array with loop answer provided by kushal could be a helpful starting point.
You would need to execute the statements separately. I do not believe the mysqli_query() function supports multiple statement.
$query[] = "DELETE FROM myTable1 WHERE indexID = '".$myVar."' LIMIT 1; ";
$query[] = "DELETE FROM myTable2 WHERE indexID = '".$myVar."' LIMIT 1; ";
$query[] = "DELETE FROM myTable3 WHERE indexID = '".$myVar."' LIMIT 6; ";
foreach($query as $q) {
$result = mysqli_query($db, $q);
}

Categories