I'm planning on developing a CMS using PHP and MySQL that utilises MySQL stored procedures to perform prepared statement queries to my MySQL database. It's been a long time since I developed in PHP (back in the procedural days) so I'm going to try and implement this system using the new OOP structure of PHP. Before I do that, I need to get to grips with the simple issue of returning the output parameter of my MySQL stored procedure to a simple php page. This is simply a test so that I can get the syntax correct before developing my first php class so the initial code posted here is procedural.
First, there is my stored procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS `text_development`.`get_user`$$
CREATE PROCEDURE `text_development`.`get_user`
(
IN userId INT,
OUT user_name VARCHAR(100)
)
BEGIN
SELECT username
INTO user_name
FROM user
WHERE user_id = userId;
END $$
DELIMITER ;
Then there is my two php files:
<?php
//db_config.php
$mysqli_host = "localhost";
$mysqli_user = "root"; //I'm not stupid enough to use this in development
$mysqli_pass = "root"; //before anyone comments about the root usage
$mysqli_db = "text_development";
?>
<?php
//index.php
require('incl/db_config.php');
$dbConnection = new Mysqli($mysqli_host, $mysqli_user, $mysqli_pass, $mysqli_db) or die(mysql_error());
print '<h1>Stored Procedure Retrieval Test</h1>';
$id = 1;
$return = '';
$result = $dbConnection->query( 'CALL get_user($id,$return)');
print $result;
?>
My problem arises with the index.php page. It doesn't actually seem to return anything. When executing the stored procedure in PHPMyAdmin the username test is returned when I pass in the user id of 1 however nothing is returned to the screen when calling the function in php. I've also tried printing $return as well but this just returns an empty string (as I define in the code above). I've tried using this online tutorial but the solutions there do not seem to work :
http://www.joeyrivera.com/2009/using-mysql-stored-procedures-with-php-mysqlmysqlipdo/
Does anyone know what I'm doing wrong here and why the return parameter of this stored procedure is not being printed to the screen?
SELECT get_user($id);?