multi sql command not working into PHP - php

i have any sql command for create backup from table's fields and i'm using this bewlow command in phpmyadmin:
SQL:
CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM sale_khomsi;
UPDATE tmptable_1 SET id= NULL , faal= 1;
INSERT INTO sale_khomsi SELECT * FROM tmptable_1;
DROP TEMPORARY TABLE IF EXISTS tmptable_1;
this work correctly but after runing this command into php code such az:
PHP:
$reslut=mysql_query("CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM sale_khomsi;
UPDATE tmptable_1 SET id= NULL , faal= 1;
INSERT INTO sale_khomsi SELECT * FROM tmptable_1;
DROP TEMPORARY TABLE IF EXISTS tmptable_1;");
not working .
result of MYSQL into PHPMYADMIN:
CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM sale_khomsi;# 4 rows affected.
UPDATE tmptable_1 SET id= NULL , faal= 1;# 4 rows affected.
INSERT INTO sale_khomsi SELECT * FROM tmptable_1;# 4 rows affected.
DROP TEMPORARY TABLE IF EXISTS tmptable_1;# MySQL returned an empty result set (i.e. zero rows).

There is no option for executing multi-query in mysql. But if you go for mysqli it is there.
mysqli_multi_query()
But if want to use mysql only mean you can go for procedure for these kind of things.

phpMyAdmin uses ; as a delimiter between calls to the database. Therefore entering that string in phpMyAdmin will cause 4 queries to run, not one massive query as you have. You need to break it down.
$result = mysql_query("CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM sale_khomsi");
$result = mysql_query("UPDATE tmptable_1 SET id= NULL , faal= 1");
$result = mysql_query("INSERT INTO sale_khomsi SELECT * FROM tmptable_1");
$result = mysql_query("DROP TEMPORARY TABLE IF EXISTS tmptable_1");
That should give you the desired result. Of course you need to check the outcome of result to make sure the query was successful.

Related

How to duplicate a row in the same table in prestashop 1.6

I know I am supposed to create a temporary table based on an existing row, remove the id and then insert an new row in my "real" table based on the temporary table. But it does not work in my prestashop.
$id = 4;
$createTemp = Db::getInstance()->Execute('CREATE TABLE temp_table AS SELECT * FROM my_table WHERE id="'.$id.'"');
$updateTemp = Db::getInstance()->Execute('UPDATE temp_table SET id=NULL');
$insertQuery = Db::getInstance()->Execute('INSERT INTO my_table SELECT * FROM temp_table');
$deleteTemp = Db::getInstance()->Execute('DROP TABLE temp_table');
If I make a var_dump of those I always get a FALSE. I tried to make the select only in a query and it does work so my SELECT * FROM my_table WHERE id="'.$id.'" is correct.
I need to be able to duplicate a row and I could'nt find another/a better way to do so. What do I have to change in my code to make it work ?
If source data is OK your code should work,
I would enable Prestashop mode_dev so you can see the query errors on-screen for better debug.
Since you're working with Prestashop - maybe your "my_table" id is not called id but something different like "id_product" ?
If this is the case, first create will fail and all other queries will fail consequently.

How to insert data in SQL temporary table from stored procedure in one query using PHP and drop table after getting resultset?

I have to execute these queries in PHP to get the result
CREATE TABLE #tempDept(deptid int)
INSERT INTO #tempDept
EXEC sprinter_DeptHier1 22;
# This will create a temporary table and insert the result of stored procedure into the temp table
select *
from sprinter_leave_request slr
where slr.id not in (select lr.leave_request_id
from leave_approver lr
where lr.approver_id = 980)
// get userid from session and userid in (select userid from userinfo
where defaultdeptid in (select deptid from #tempDept)) and slr.userid
<> 980 order by userid;
# This query will get the result from temp table
And after getting the result I have to drop the table before next transition occurs.
Kindly help me out to solve the issue, I have to execute the query as same as in the question.
I tried so but have no luck.

execute multiple query with temp table in php

I have the following query, to copy a row within a table and alter a few columns.
CREATE TEMPORARY TABLE temp_table AS
SELECT *
FROM table1
WHERE offertecode = '1c12a23453453458e492230df420972';
UPDATE temp_table
SET offertecode = '82a24c7da2342423424351804ab043',
id = NULL,
reference = '[COPY] subject';
INSERT INTO table1
SELECT *
FROM temp_table;
DROP TEMPORARY TABLE temp_table;
This works perfectly fine in phpmyadmin, but I cant get it to work from within PHP, I get an error:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax
to use near 'UPDATE temp_table SET offertecode = '82a24c7da2342423424351804ab043',
id = ' at line 5
Can anyone help me on how to execute this query in PHP?
PHP code:
$mysqli->query("CREATE TEMPORARY TABLE temp_table AS
SELECT *
FROM table1
WHERE offertecode = '1c12a23453453458e492230df420972';
UPDATE temp_table
SET offertecode = '82a24c7da2342423424351804ab043',
id = NULL,
reference = '[COPY] subject';
INSERT INTO table1
SELECT *
FROM temp_table;
DROP TEMPORARY TABLE temp_table;");
Thanks!
This doesn't address the mysqli problem (which is having four statements in a single query). You should run those separately. But, you don't need four statements. Just do:
insert into table1(offertecode, id, reference, <rest of columns>)
select '82a24c7da2342423424351804ab043' as offertecode, NULL as id, '[COPY] subject' as reference,
<rest of columns>
from table1
where offertecode = '1c12a23453453458e492230df420972';
An insert . . . select statement can refer to the same table in both parts. Even in MySQL.
You need to use $mysqli->multi_query. The basic answer is you cannot run multiple queries using $mysqli->query. The syntax error is due to a second query trying to to be run under $mysqli->query which only allows one main query. That query can contain sub or nested queries, but only one main query. To run multiple queries you have to use $mysqli->multi_query

insert into table as select * from table does not work

This SQL statement is used in PHP:
Insert into Table from AS Select * from Tabl1 where id='5'
Any clues to why it is not inserting values from Table1 to Table? User executes this statement to create a copy.
Remove the "FROM AS" and it should work. See http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html
Remove from as:
Insert into Table (Select * from Tabl1 where id='5')
(and BTW why '5' and not just 5? Is id actually a string value?)

MySQL Queries overlapping each other

I'm having an issue with MySQL Queries, there are a couple queries that seem to be overlapping each other. Basically, I have some PHP code (being called via a REST API) using MySQL with PDO that inserts a value into a table if it doesn't already exist. If it exists, then it updates the record instead. Pretty simple stuff, and it seems to be working 99% of the time. But on certain occasions the same record gets inserted twice (instead of being updated the second time).
I looked into the MySQL log and found that on rare occasions the script is somehow being run twice at the same time (causing overlapping queries). Notice how the process IDs 95587 and 95588 seem to be overlapping.
95587 Connect #localhost on ****
95588 Connect #localhost on ****
95587 Query SET NAMES utf8
95588 Query SET NAMES utf8
95588 Query SELECT * FROM `my_table` WHERE `data` = '89158268' LIMIT 1
95587 Query SELECT * FROM `my_table` WHERE `data` = '89158268' LIMIT 1
95588 Query INSERT INTO `my_table` (`data`, `date_created`, `date_updated`) VALUES ('89158268', '2011-08-21 17:11:10 ', '2011-08-21 17:11:10 ')
95587 Query INSERT INTO `my_table` (`data`, `date_created`, `date_updated`) VALUES ('89158268', '2011-08-21 17:11:10 ', '2011-08-21 17:11:10 ')
95588 Quit
95587 Query SELECT * FROM `another_table`
95587 ... more queries
This is the order of the code:
SELECT * FROM my_table WHERE data = '89158268' LIMIT 1
if record found, then update my_table
else if record not found, insert new record into my_table and then continue other queries
Can anyone think of a reason why these 2 mysql processes seem to be overlapping each other? Any help is greatly appreciated. Thank you.
Try locking the table before the select:
LOCK TABLE my_table WRITE;
SELECT * FROM `my_table` WHERE `data` = '89158268' LIMIT 1;
INSERT INTO `my_table` (`data`, `date_created`, `date_updated`);
UNLOCK TABLES;
This will avoid any other MySQL session from using the my_table table, and avoid this race condition.

Categories