SQL/php - Stored procedures - One procedure bugs the rest of my code!
SQL version - 5.0.77 Using PHPMyAdmin
Stored procedure code:
CREATE PROCEDURE checkStudent (IN email VARCHAR(50))
BEGIN
SELECT email, name, gender, dob, status
FROM studentCOMP
WHERE studentCOMP.email = email;
END$$
P开发者_StackOverflow社区HP code:
//$exist = mysql_query("SELECT * FROM studentCOMP WHERE (email ='$email')");
$exist = mysql_query("CALL checkStudent('$email')");
$rowresult = mysql_num_rows($exist);
if($rowresult == 0) //if 0 results are returned from the earlier query
{
echo "Please enter a valid email address- Student does not exist";
}
else
{
$exams = mysql_query("
SELECT StudentExamLink.examID, examCOMP.date, examCOMP.medallevel, examCOMP.mark, examCOMP.dancestyle
FROM StudentExamLink
INNER JOIN examCOMP
ON StudentExamLink.examID=examCOMP.examID
WHERE (StudentExamLink.email ='$email')
");
$rowresult2 = mysql_num_rows($exams);
echo "$rowresult2 number of rows";
}
if ($rowresult2 == 0)
{
echo "no exams taken";
}
Hi I was wondering if anyone could help with my problem. When I run the code above with
$exist = mysql_query("SELECT * FROM studentCOMP WHERE (email ='$email')");
replacing the stored procedure everything works fine. But when I substitute in the Stored Procedure, it causes the piece of code
$exams = mysql_query("
SELECT StudentExamLink.examID, examCOMP.date, examCOMP.medallevel, examCOMP.mark, examCOMP.dancestyle
FROM StudentExamLink
INNER JOIN examCOMP
ON StudentExamLink.examID=examCOMP.examID WHERE (StudentExamLink.email ='$email')
");
to return no rows, even though it should return some. Is the stored procedure locking up the table or something? Im not sure how to fix it, When ever I do it on other pages it also messes up subsequent queries.
Thanks in advance
I've never had much love from stored procedures that return result sets.
Better to create a temporary table in your stored procedure and select from that in your php code.
CREATE PROCEDURE checkStudent (IN email VARCHAR(50))
BEGIN
CREATE TEMPORARY temptable
SELECT email, name, gender, dob, status
FROM studentCOMP
WHERE studentCOMP.email = email;
END$$
And then
$query = "SELECT * FROM temptable";
$exams = mysql_query($query);
精彩评论