trying to create a stored procedure through mysqli in php. Keep throwing back at 1064 error when I do it through php, but is fine, when I copy the same sql command in php. Anyone able to shed some light on what could be the problem, or anyone found a solution to something similar?
Create Procedure code:
DELIMITER //
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN taskID INT, IN avaliableTime INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE cur1 CURSOR FOR SELECT t.`task_id`
FROM `task_dependency` d
LEFT OUTER JOIN (
SELECT `task_id`, `dependent_on_task_id`, ti.`id`, min(ti.`completed`) as make_live
FROM `task_dependency`
LEFT OUTER JOIN (
SELECT `task_id`, `completed` FROM `6de443aacf727f6009e857480f33153c_tasks`
) ti ON ti.`task_id` = `dependent_on_task_id`
GROUP BY `task_id`
) t ON d.`task_id` = t.`task_id`
WHERE d.`dependent_on_task_id` = taskID
AND t.make_live > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `live` = 1 WHERE `task_id` = a;
END LOOP;
CLOSE cur1;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `earliest_start` = avaliableTime WHERE `task_id` IN (
SELECT `task_id`
FROM `task_dependency`
WHERE `dependent_on_task_id` = taskID
) AND `earliest_start` < avaliableTime;
END //
DELIMITER ;
PHP Code:
if (!$this->dbc->query("DROP PROCEDURE IF EXISTS " . $this->table_prefix . "makeLive") ||
!$this->dbc->query($query)) {
echo "Stored procedure creation failed: (" . $this->dbc->errno . ") " . $this->dbc->error . "<br /><br />" . $query;
exit;
}
echo "success";
exit;
Script execution is throwing out the following error...
Stored procedure creation failed: (1064) 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 'DELIMITER //
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN tas' at line 1
Although it says it's a syntax error, the fact a straight copy and paste of the command to phpmyadmin seems to work, looks like it's gotta be some sort of mysqli bug perhaps?
The query function only allows you to execute a single query. Try the multi_query function (documentation: http://www.php.net/manual/en/mysqli.multi-query.php ) instead. It allows for multiple commands, and may fix your issue.
Turns out mysqli->query already parses semi-colons and runs the whole command. The delimiter commands at the start and end of the query where unnecessary in successfully creating the procedure.
Just remove DELIMITER and it will work. So in your case it will be like this:
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN taskID INT, IN avaliableTime INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE cur1 CURSOR FOR SELECT t.`task_id`
FROM `task_dependency` d
LEFT OUTER JOIN (
SELECT `task_id`, `dependent_on_task_id`, ti.`id`, min(ti.`completed`) as make_live
FROM `task_dependency`
LEFT OUTER JOIN (
SELECT `task_id`, `completed` FROM `6de443aacf727f6009e857480f33153c_tasks`
) ti ON ti.`task_id` = `dependent_on_task_id`
GROUP BY `task_id`
) t ON d.`task_id` = t.`task_id`
WHERE d.`dependent_on_task_id` = taskID
AND t.make_live > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `live` = 1 WHERE `task_id` = a;
END LOOP;
CLOSE cur1;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `earliest_start` = avaliableTime WHERE `task_id` IN (
SELECT `task_id`
FROM `task_dependency`
WHERE `dependent_on_task_id` = taskID
) AND `earliest_start` < avaliableTime;
END;
Related
Here is the reference link that I used to create procedures: https://dba.stackexchange.com/questions/7147/find-highest-level-of-a-hierarchical-field-with-vs-without-ctes/7161#7161?newreg=88de047579d242c98b8cfe0e17f0330f
Procedures are working well with the example. But not working in my own database. Seems I do something wrong while creating procedures.
Here is the procedure for my db:
DELIMITER $$
DROP FUNCTION IF EXISTS `demo`.`GetParentIDByID` $$
CREATE FUNCTION `demo`.`GetParentIDByID` (GivenID INT) RETURNS INT
DETERMINISTIC
BEGIN
DECLARE rv INT;
SELECT IFNULL(user_by,-1) INTO rv FROM
(SELECT user_by FROM users WHERE id = GivenID) A;
RETURN rv;
END $$
DELIMITER ;
Sql query for the following procedure is working fine. It is basically getting parent_id of the node:
SELECT id,GetParentIDByID(id) FROM users;
Issue occurs when I am try to get Ancestors. Here is the Procedure that I am using:
DELIMITER $$
DROP FUNCTION IF EXISTS `demo`.`GetAncestry` $$
CREATE FUNCTION `demo`.`GetAncestry` (GivenID INT) RETURNS VARCHAR(1024)
DETERMINISTIC
BEGIN
DECLARE rv VARCHAR(1024);
DECLARE cm CHAR(1);
DECLARE ch INT;
SET rv = '';
SET cm = '';
SET ch = GivenID;
WHILE ch > 0 DO
SELECT IFNULL(user_by,-1) INTO ch FROM
(SELECT user_by FROM users WHERE id = ch) A;
IF ch > 0 THEN
SET rv = CONCAT(rv,cm,ch);
SET cm = ',';
END IF;
END WHILE;
RETURN rv;
END $$
DELIMITER ;
SQL query:
SELECT id,GetAncestry(id) FROM users;
Phpmyadmin getting stuck, if I am using this query. It never returns me anything nor an error or success
I have written a stored procedure in mysql which will create a TEMPORARY TABLE, I want to access the data of TEMPORARY TABLE using Codeigniter.But when I call "$this->db->query()" it returns empty data.
$data=array();
$call_procedure = "CALL sp_Stock()";
$query = $this->db->query($call_procedure);
$sql="SELECT * FROM StockTable";
$query1 = $this->db->query($sql);
I have changed my way to show the data. And I do changes it on stored procedure.
DELIMITER $$
CREATE PROCEDURE sp_Stock()
BEGIN
DECLARE cursor_finish INTEGER DEFAULT 0;
DECLARE m_numofpurchaseBag DECIMAL(10,2)DEFAULT 0;
DECLARE m_purchasedKg DECIMAL(10,2)DEFAULT 0;
DECLARE m_purBagDtlId INTEGER DEFAULT 0;
DECLARE stockCursor CURSOR FOR
SELECT purchase_bag_details.`actual_bags`,
(purchase_bag_details.`net`*purchase_bag_details.`actual_bags`) AS PurchasedKg,
purchase_bag_details.`id` AS PurchaseBagDtlId
FROM
purchase_invoice_detail
INNER JOIN
purchase_bag_details
ON purchase_invoice_detail.`id`= purchase_bag_details.`purchasedtlid`
INNER JOIN
`do_to_transporter`
ON purchase_invoice_detail.`id` = do_to_transporter.`purchase_inv_dtlid`
WHERE purchase_invoice_detail.`teagroup_master_id`=6
AND purchase_invoice_detail.`id`=1481
AND do_to_transporter.`in_Stock`='Y';
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET cursor_finish = 1;
DROP TEMPORARY TABLE IF EXISTS StockTable;
#temptable creation
CREATE TEMPORARY TABLE IF NOT EXISTS StockTable
(
purchaseBagDtlId INT,
purchasedBag NUMERIC(10,2),
purchasedKg NUMERIC(10,2),
blendedBag NUMERIC(10,2),
blendedKg NUMERIC(10,2),
stockBag NUMERIC(10,2),
stockKg NUMERIC(10,2)
);
#temptable creation
OPEN stockCursor ;
get_stock : LOOP
FETCH stockCursor INTO m_numofpurchaseBag,m_purchasedKg,m_purBagDtlId;
IF cursor_finish = 1 THEN
LEAVE get_stock;
END IF;
/*SELECT m_numofpurchaseBag,m_purchasedKg,m_purBagDtlId; */
/* Blending bag query*/
SET #m_numberofBlndBag:=0;
SET #m_BlndKg:=0;
/* Blend bag*/
SELECT #m_numberofBlndBag:=SUM(blending_details.`number_of_blended_bag`) AS belendedBag INTO #m_numberofBlndBag
FROM blending_details
WHERE blending_details.`purchasebag_id`= m_purBagDtlId
GROUP BY
blending_details.`purchasebag_id`;
#Blend Bag
#Blend Kgs
SELECT #m_BlndKg:=SUM(blending_details.`qty_of_bag` * blending_details.`number_of_blended_bag`) AS blendkg INTO #m_BlndKg
FROM blending_details
WHERE blending_details.`purchasebag_id`= m_purBagDtlId
GROUP BY
blending_details.`purchasebag_id`;
SET #m_StockBag:=(m_numofpurchaseBag - #m_numberofBlndBag);
SET #m_StockKg:=(m_purchasedKg - #m_BlndKg);
INSERT INTO StockTable
(
purchaseBagDtlId ,
purchasedBag ,
purchasedKg ,
blendedBag ,
blendedKg ,
stockBag ,
stockKg
)VALUES(m_purBagDtlId,m_numofpurchaseBag,m_purchasedKg,#m_numberofBlndBag,#m_BlndKg,#m_StockBag,#m_StockKg);
END LOOP get_stock;
CLOSE stockCursor;
SELECT * FROM StockTable;
#DROP TABLE StockTable;
END$$
DELIMITER ;
#CALL sp_Stock();
Anywhere I am using a temp table I am executing this before the temp table is created:
$this->db->query('DROP TABLE IF EXISTS StockTable');
I seem to remember reading someone having the same problem as you and for some reason you have to execute the above first.
So try:
$data=array();
$call_procedure = "CALL sp_Stock()";
$this->db->query('DROP TABLE IF EXISTS StockTable');
$query = $this->db->query($call_procedure);
$sql="SELECT * FROM StockTable";
$query1 = $this->db->query($sql);
How do I use php and mysql to create the following trigger for each table created.
I want to convert the following code to be used as a sql query from php.
DELIMITER $$
CREATE TRIGGER trigger1
BEFORE INSERT
ON table1
FOR EACH ROW
BEGIN
SELECT COUNT(*) INTO #cnt FROM table1;
IF #cnt >= 25 THEN
CALL sth();
END IF;
END
$$
DELIMITER ;
I tried to use the following but it does not work.
if ($method == "T")// method is activated if $_GET["method'] = T method is set to T in script to allways return true
{
$sql = "CREATE TRIGGER TRIGGER1
BEFORE INSERT
ON 49a64d6512
FOR EACH ROW
BEGIN
SELECT COUNT(*) INTO #cnt FROM 49a64d6512;
IF #cnt >=25 THEN
CALL sth();
END IF;
END";
echo '1';
}
Is what i am trying to do even possible?
I am getting this error:
#1064 - 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 '' at line 4
when i run followed code in phpmyadmin also appearing this error beside of second declare expression:
unrecognized statement type (near declare)
What can be cause of these error? (Phpmyadmin, version, etc)
create function fnc_generate_url(title_in varchar(250))
returns varchar(250)
begin
declare v_count int;
declare v_return varchar(250);
declare cr_count cursor
for
select count(1) from tbl_page where page_title like concat('%',title_in,'%');
open cr_count;
fetch cr_count into v_count;
close cr_count;
if v_count = 0 then
set v_return = replace(trim(title_in), ' ', '-');
else
set v_return = concat(replace(trim(title_in), ' ', '-'),'-',v_count);
end if;
return v_return;
end;
Works fine if we use delimiters.
Try this:
DELIMITER $$
create function fnc_generate_url(title_in varchar(250))
returns varchar(250)
begin
declare v_count int;
declare v_return varchar(250);
declare cr_count cursor
for
select count(1) from tbl_page where page_title like concat('%',title_in,'%');
open cr_count;
fetch cr_count into v_count;
close cr_count;
if v_count = 0 then
set v_return = replace(trim(title_in), ' ', '-');
else
set v_return = concat(replace(trim(title_in), ' ', '-'),'-',v_count);
end if;
return v_return;
end;
$$
DELIMITER ;
I want to do a Migration data from one table to another.
I wrote a simple PHP script for my purposes but I wanted to do this by MySql script using user defined variables:
the PHP script looks like this:
//MIGRATION
$sql = "SELECT position FROM ts_user_config WHERE position != '' AND position NOT REGEXP '^-?[0-9]+$' GROUP BY TRIM(position) ORDER BY position";
$positions = db_loadColumn( $sql );
foreach ($positions as $key => $pos) {
$sql = "SELECT id FROM user_positions where UPPER(position) = UPPER('$pos')";
$posId = db_loadResult($sql);
if ($posId == null) {
$sql = "INSERT INTO user_positions (position, `desc`) VALUES ('$pos', '$pos')";
db_exec($sql);
$posId = db_insert_id();
}
$sql = "UPDATE ts_user_config SET position='$posId' WHERE TRIM(position)='$pos'";
db_exec($sql);
}
//---------
Could somebody be so kind and rewrite this PHP instructions to MySQL script? I tried to do this but my mySQL knowledge is very low and I couldn't done that.
Please help me if its not too much effort.
Thank you in advance.
I Have DONE IT !!!!!! :)
this is my mySQL script, I dont know if its perfect but its do what I need. Please tell me if I can make something better here. Thanks again :)
drop procedure if exists PositionMigration;
delimiter '//'
CREATE PROCEDURE PositionMigration()
BEGIN
BLOCK1: BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE pos VARCHAR(100);
DECLARE posId1 INT;
DECLARE posId2 INT;
DECLARE sql1 CURSOR FOR SELECT position FROM ts_user_config WHERE position != '' AND position NOT REGEXP '^-?[0-9]+$' GROUP BY TRIM(position) ORDER BY position;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN sql1;
read_loop: LOOP
FETCH sql1 INTO pos;
IF done THEN
LEAVE read_loop;
END IF;
BLOCK2: BEGIN
DECLARE posNotFound INT DEFAULT FALSE;
DECLARE sql2 CURSOR FOR SELECT id FROM user_positions where UPPER(position) = UPPER(pos);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET posNotFound = TRUE;
OPEN sql2;
FETCH sql2 INTO posId1;
IF posNotFound THEN
INSERT INTO user_positions (position, \`desc\`) VALUES (pos, pos);
BLOCK3: BEGIN
DECLARE sql3 CURSOR FOR SELECT LAST_INSERT_ID();
OPEN sql3;
FETCH sql3 INTO posId2;
UPDATE ts_user_config SET position=posId2 WHERE TRIM(position)=pos;
CLOSE sql3;
END BLOCK3;
ELSE
UPDATE ts_user_config SET position=posId1 WHERE TRIM(position)=pos;
END IF;
CLOSE sql2;
END BLOCK2;
END LOOP;
CLOSE sql1;
END BLOCK1;
END;
//
delimiter ';'
call PositionMigration();