Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get scalar result from prepared statement?

Is it possible to set the result from a prepared statement into a variable? I am trying to create the following stored procedure but it is failing:

ERROR 1064 (42000) at line 31: 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 'stmt USING @m, @c, @a;

DROP PROCEDURE IF EXISTS deleteAction;

DELIMITER $$
CREATE PROCEDURE deleteAction(
    IN modul CHAR(64),
    IN controller CHAR(64),
    IN actn CHAR(64))

MODIFIES SQL DATA

BEGIN

    PREPARE stmt FROM 'SELECT id 
                         FROM actions 
                        WHERE `module` = ? 
                          AND `controller` = ? 
                          AND `action` = ?';

    SET @m = modul;
    SET @c = controller;
    SET @a = actn;

    SET @i = EXECUTE stmt USING @m, @c, @a;

    DEALLOCATE PREPARE stmt;

    DELETE FROM acl WHERE action_id = @i;
    DELETE FROM actions WHERE id = @i; 

END 
$$
DELIMITER ;
like image 890
Matt McCormick Avatar asked Oct 21 '10 22:10

Matt McCormick


People also ask

Can we use prepared statement for SELECT query?

You must always use prepared statements for any SQL query that would contain a PHP variable. To do so, always follow the below steps: Create a correct SQL SELECT statement.

What is prepare () in PHP?

A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. Prepared statements basically work like this: Prepare: An SQL statement template is created and sent to the database. Certain values are left unspecified, called parameters (labeled "?").

What is the SQL query to release the prepared statement?

MySQL prepared statement usage EXECUTE – execute a prepared statement prepared by the PREPARE statement. DEALLOCATE PREPARE – release a prepared statement.


2 Answers

It may seem strange, but you can assign the variable directly in the prepared statement string:

PREPARE stmt FROM 'SELECT @i := id FROM ...';

-- ...

EXECUTE stmt USING @m, @c, @a;

-- @i will hold the id returned from your query.

Test case:

CREATE TABLE actions (id int, a int);

INSERT INTO actions VALUES (1, 100);
INSERT INTO actions VALUES (2, 200);
INSERT INTO actions VALUES (3, 300);
INSERT INTO actions VALUES (4, 400);
INSERT INTO actions VALUES (5, 500);

DELIMITER $$
CREATE PROCEDURE myProc(
    IN p int
)

MODIFIES SQL DATA

BEGIN

    PREPARE stmt FROM 'SELECT @i := id FROM actions WHERE `a` = ?';

    SET @a = p;

    EXECUTE stmt USING @a;

    SELECT @i AS result;

    DEALLOCATE PREPARE stmt;

END 
$$
DELIMITER ;

Result:

CALL myProc(400);

+---------+
| result  |
+---------+
|       4 |
+---------+
1 row in set (0.00 sec)
like image 136
Daniel Vassallo Avatar answered Nov 01 '22 20:11

Daniel Vassallo


Use this code

PREPARE stmt FROM 'SELECT ''a'' into @i' ;

EXECUTE stmt;

if(@i='a') then
............
end if;
like image 29
XEENA Avatar answered Nov 01 '22 22:11

XEENA