October 22, 2024
Chicago 12, Melborne City, USA
PHP

Stored Procedures, MySQL and PHP


I’ve been doing what I’m about to explain using ASP classic and ASP.Net with MSSQL Server for all that time and it works beautifully!

Here below is the simplest example, one of MANY, and connection methods I have tried that utterly fails, just like all the rest. The SP works in MariaDb when Executed, but the data is never returned or at least the variable $value is never updated in PHP. In all examples I’ve tried, I’ve been able to verify that the data is reaching the SP and everything works fine there.

According to all the examples I have found, this should work, but not for me. What could possibly be the problem? I’m just not seeing or finding it. I must be lacking knowledge somewhere.
config.php

<?php

$dsn = "mysql:host=localhost;dbname=rolodex;charset=utf8mb4";

$options = [
  PDO::ATTR_EMULATE_PREPARES   => false, // Disable emulation mode for "real" prepared statements
  PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION, // Disable errors in the form of exceptions
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // Make the default fetch be an associative array
];

try {
  $pdo = new PDO($dsn, "dbuser", "dbuserpassword", $options);
}
catch (Exception $e) {
  error_log($e->getMessage());
  exit('Something bad happened');
}

?>

pdocontacts.php

<?php

// Include the database connection file
include_once("config.php");

// PHP class 
class Contact {
      public $id;
      public $name;
      public $age;
      public $email;
}

// Fetch contacts (in descending order)
$contacts = $pdo->query( "SELECT * FROM contacts ORDER BY id DESC")->fetchAll(PDO::FETCH_CLASS, 'Contact'); // This returns an array of data inserted.
var_dump($contacts);

$stmt = $pdo->prepare("CALL sp_takes_string_returns_string(?)");
$value="hello";
$stmt->bindParam(1, $value, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000); 

// call the stored procedure
$stmt->execute();

print "procedure returned $value<br>";

?>

The MariaDb Stored Procedure sp_takes_string_returns_string:

DELIMITER $$
CREATE PROCEDURE `sp_takes_string_returns_string`(INOUT `astr` VARCHAR(100))
BEGIN
    INSERT contacts (name, age, email) VALUES (astr, 1, 'im@mywittsend.com');
    SELECT REVERSE(astr) INTO astr;
END$$
DELIMITER ;

pdocontacts.php output:

array(5) { \[0\]=\> object(Contact)#3 (4) { \["id"\]=\> int(5) \["name"\]=\> string(5) "hello"
\["age"\]=\> int(1) \["email"\]=\> string(15) "im@mywittsend.com" } \[1\]=\> object(Contact)#4 (4)
{ \["id"\]=\> int(4) \["name"\]=\> string(5) "hello" \["age"\]=\> int(1) \["email"\]=\> string(15)
"im@mywittsend.com" } \[2\]=\> object(Contact)#5 (4) { \["id"\]=\> int(3) \["name"\]=\> string(5)
"hello" \["age"\]=\> int(1) \["email"\]=\> string(15) "im@mywittsend.com" } \[3\]=\>
object(Contact)#6 (4) { \["id"\]=\> int(2) \["name"\]=\> string(5) "hello" \["age"\]=\> int(1)
\["email"\]=\> string(15) "im@mywittsend.com" } \[4\]=\> object(Contact)#7 (4) { \["id"\]=\> int(1)
\["name"\]=\> string(5) "hello" \["age"\]=\> int(1) \["email"\]=\> string(15) "im@mywittsend.com" }
}
procedure returned hello

I even swapped the insert and reverse to show that the reverse is working.

SELECT REVERSE(astr) INTO astr;
INSERT contacts (name, age, email) VALUES (astr, 1, 'im@wittsend.com');

After a couple executions of the page:

array(7) { [0]=> object(Contact)#3 (4) { ["id"]=> int(7) ["name"]=> string(5) "olleh"
["age"]=> int(1) ["email"]=> string(15) "im@wittsend.com" } [1]=> object(Contact)#4 (4) {
["id"]=>...
**procedure returned hello**

You can see that ‘hello’ or ‘olleh’ is being inserted into contacts as the name.
What flag or whatever am I missing???



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video