I am new to writing MySQL stored procedures and I am having some issues.
This is the stored procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS login$$
CREATE PROCEDURE login(
IN usern VARCHAR(11),
IN pw VARCHAR(30),
OUT msg VARCHAR(50)
)
login_proc:
BEGIN
SELECT COUNT(*) as voter
FROM regVoter
WHERE studentID = usern AND password = pw;
IF voter = 1 THEN
SET msg = "login successfully";
ELSE
SET msg = "login failed";
END IF;
LEAVE login_proc;
END $$
DELIMITER ;
The following is the PHP file in which the stored procedure is to be called:
<?php
ini_set('display_errors','1'); error_reporting(E_ALL);
include_once 'dbConnect.php';
$username = $_POST['username'];
$password = $_POST['password'];
if (connect()){
global $conn;
$query = $conn->query("CALL login('$username','$password',@msg)") or die("Query failed: " . $conn->error);
$result = $conn->query("SELECT @msg AS data");
$row = $result->fetch_assoc();
echo $row['data'];
}
else{
$data = "connection error!";
}
?>
The error I am getting is "Uncaught Error: Call to a member function fetch_assoc() on boolean" which means one of the queries is returning false.
What am I doing wrong here?