MySQL stored procedure: cannot be executed with PHP code

I have the following stored procedure to check username availability

DELIMITER $$;

DROP PROCEDURE IF EXISTS tv_check_email$$

CREATE PROCEDURE tv_check_email (IN username varchar(50))
BEGIN
  select USER_ID from tv_user_master where EMAIL=username;
END$$

DELIMITER ;$$

When I run this from my MySQL frontend, it works fine:

call tv_check_email('shyju@techies.com')

But when I try to execute from a PHP page, I get an error, for example

"PROCEDURE mydatabase.tv_check_email can't return a result set in the given context"

I am sure my version of PHP is 5.2.6.

+3
source share
3 answers

Cody is not 100% right. You can link the resulting result columns and return the selection data from the stored procedure.

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$stmt = $mysqli->prepare("call tv_check_email(?)");
$stmt->bind_param('s', "shyju@techies.com");
$stmt->execute();

$stmt->bind_result($userid);

while ($stmt->fetch()) {
  printf("User ID: %d\n", $userid);
}

$stmt->close();
$mysqli->close();
+3
source

You need to bind your result to the OUT parameter.

See mysql docs in stored procedures

mysql> delimiter //

mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
    -> BEGIN
    ->   SELECT COUNT(*) INTO param1 FROM t;
    -> END;
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;

mysql> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @a;
+------+
| @a   |
+------+
| 3    |

+ ------ +

+3
source

Source: https://habr.com/ru/post/1706160/


All Articles