I want to run multiple mysql queries from one php page but I need it to "wait" until each query is finished before moving on. It seems that at the moment it will load them all at the same time.
Is there a work around for this or a function to make the page wait until the query is done before going to the next one?
--edited--
so here is my code
//These top three seem to work fine but I guess it doesn't matter if the next one starts before the last one ends
mysqli_query($con,"DELETE\n".
"FROM\n".
" temp_cats");
mysqli_query($con,"DELETE\n".
"FROM\n".
" temp_concats");
mysqli_query($con,"DELETE\n".
"FROM\n".
" temp_locations");
That seems to work OK but I wouldn't notice if one starts before the one before ends.
The next query works fine. It takes countrys mentioned in the query from another table and adds it to a new table. The update query "labels" them in another column.
mysqli_query($con,"INSERT IGNORE temp_locations (temp_locations.Location) SELECT DISTINCT\n".
"Region\n".
"FROM\n".
"LocationsForExcel\n".
"WHERE\n".
"Country IN (" . $_SESSION['SearchCountriesQuery'] . ")");
//mark the new locations type as country
mysqli_query($con,"UPDATE temp_locations\n".
"SET LocationType = 'Country'\n".
"WHERE\n".
" LocationType IS NULL");
The problems come when I put this underneath:
mysqli_query($con,"INSERT IGNORE temp_locations (temp_locations.Location) SELECT DISTINCT\n".
"Region\n".
"FROM\n".
"LocationsForExcel\n".
"WHERE\n".
"Country IN" . $_SESSION['SearchCountriesQuery'] . ")");
//mark the new locations type as region
mysqli_query($con,"UPDATE temp_locations\n".
"SET LocationType = 'Region'\n".
"WHERE\n".
" LocationType IS NULL");
The end result is that both of the INSERT queries are running fine but the 'LocationType' column are all showing as 'Country'.
The only thing that I can think is happening is that the queries are not running in order or are over lapping each other.
Does anyone see whats going on here?
This is not a realy good method of doing it but what you could to is this (Showing some code would help a lot):
<?php
$query = //your query
if($query) {
$query2 = //your 2nd query
}
if($query2) {
$query3 = //your 3rd query
}
//and so on
?>
Related
I'm looking to enter in a whole object of information. The data itself needs to be put into different tables so I'm using a mysqli_multi_query
Query
for($i=0;$i<$commLength;$i++){
$sql = "INSERT INTO
calinfo (Sbefore, Safter, A1, A2, CalGas, Factor, Zbefore, Zafter, Cbefore, Cafter, SysInfoID)
VALUES
('{$comm[$i]->ScB4}', '{$comm[$i]->ScA4}', '{$comm[$i]->A1}', '{$comm[$i]->A2}', '{$comm[$i]->CalGasA}', '{$comm[$i]->Factor}', '{$comm[$i]->ZB4}', '{$comm[$i]->ZA4}', '{$comm[$i]->CalB4}', '{$comm[$i]->CalA4}', '{$comm[$i]->CHNoID}');";
$sql .= "UPDATE
jobs
SET
CompletedBy = $tech
WHERE JobID = '{$comm[$i]->JobID}';";
if(!mysqli_multi_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
else
{
$synctotal++;
}
}
I think I'm doing this wrong. All the data and values and being read correctly but I get this error code:
'Error: Commands out of sync; you can't run this command now'
Could someone point me in the right direction with this one please =)
Did you try running two sql queries as separate statements? rather than keeping them in 1 SQL string variable?
I've two mysql tables, here is sql for them =>
I'm making dynamic menu with its sub menu . SO I've the situation that want to execute such a query
DELETE FROM artinfo WHERE descName=(SELECT for_sub_url FROM menu WHERE menu_id=" . $_POST['main_menu_titles'] . ")
where $_POST['main_menu_titles'] exists and is menu_id. I'm not writing php code too , because it works just fine and remarkable is that , this query executes when I am trying to execute it from mysql shell (of course using directly number of menu_id instead of $_POST['m_num'])
Any ideas whats going on, how to execute it from php script ? thanks :)
UPDATE
here is php script
if ($connection->query("DELETE FROM artinfo WHERE descName=(SELECT for_sub_url FROM menu WHERE menu_id=" . $_POST['main_menu_titles'] . ")") && $connection->query("DELETE FROM artinfo WHERE descName IS NULL AND cat_id=" . $_POST['main_menu_titles'] . "")){
$edit_res_fine = "DELETED";
}
If you say the query works just fine without $_POST, I would think the code in question is located in the form you submit to acquire $_POST['main_menu_titles']. To test the correct value is being sent, try echoing the value of $_POST['main_menu_titles'] right before the query.
Try this.
$query = "DELETE FROM artinfo WHERE descName = (SELECT for_sub_url FROM menu
WHERE menu_id = '" . $_POST['m_num'] . "')";
i am trying to update my records using UPDATE in php and mysql , the query working but there is not updates at all happened on the database , i have many records for tickets which i need to update there status when the user purchase them , let say the user books 10 tickets i used this syntax
for ($counter = 1; $counter <= $tickets; $counter++) {
echo $eventId;
echo $chooseClass;
echo $chooseUser;
$bookTicket = mysql_query("UPDATE units
SET ticketSold = 'Yes',
userIdFK = '$chooseUser'
WHERE BusinessreservationIdFk = '$eventId'
AND classIDfk ='$choosedClass'"
) or die(mysql_error());
if ($bookTicket)
{
echo "<br/>ticket " . $counter . " done !";
}
else
{
i tried to echo all variables here inside the for loop to make sure this for gets all the variables values , which is working , i have like 1000 tickets already stored on mysql table units which i need to update their status from sold = No to Yes. where is the problem here ?
try building the query separately (e.g. $sql = 'UPDATE ...', so you can do an echo $sql and copy/paste the query and run it manually. Nothing in your code looks wrong, so the values you're passing around must not be correct or the WHERE ... logic isn't proper. So run a sample query manually and see if anything happens then.
However, note that you're doing this inside a for() loop, but aren't using that $counter value anywhere. In effect you're just running the SAME query over and over. Setting ticketSold to Yes $counter times isn't going to make it "more" Yes than if you'd done this update only once.
I have a self calling php form that is supposed to update the database, then display the changes. This is the general idea of what the code looks like:
IF($condition)
mysqli_multi_query($dbc,$multiple_update_query_str);
$result = mysqli_query($dbc,$select_query);
while($row = mysqli_fetch_array($result))
echo $row[0] . " " . $row[1] . " " . $row[2] . "<br>";
The first time, when the $condition is false, the select query works perfectly. Then when the $condition is true, the update occurs on the database, but the select query fails.
My first thought was that php server was getting ahead of the mySQL server, so I used sleep(5) before exiting the if statement, but the select still failed.
I even wrote a very basic php file that was almost exactly this code. It had the same problem. Is there something I am missing?
before you can use mysqli_query you must retrieve all the results of the multy_query to unlock the link connection
try:
if($condition){
mysqli_multi_query($dbc,$multiple_update_query_str);
while(mysqli_next_result($dbc)){;}
}
i have written few codes to show time spent by users at site but when a users click on submit it should be stored in mysql but its not getting stored can you please tell where i have done mistake here is my code.
Your query seems to be wrong.
Either use INSERT without WHERE if you want to insert a new record. If however you want to update an already present record, use UPDATE instead of INSERT.
And it is always a good idea to check whether a query was successful:
if (mysql_query ("insert into jcow_accounts(Time_Spent) values ('{$Time}') where uid='{$client['id']}' ") === FALSE) {
echo 'MySQL error: ' . mysql_error() . "\n";
}
You need to use an UPDATE instead of an insert.
$dtime = getChangeInTime(); // this is the change in time from the last update
mysql_query( "UPDATE jcow_accounts SET `TIME_SPENT` = `TIME_SPENT` + $dtime ".
" where id='{$client['id']}'" );
Try
insert INTO `jcow_accounts` (`Time_Spent`) VALUES ('{$Time}') where uid='{$client['id']}' WHERE `uid` = '{$client['id']}'
Are you sure the uid is in the DB? try running it without the WHERE...