SQL存储过程不会在PHP页面上执行

时间:2018-03-23 01:51:20

标签: php mysql sql stored-procedures phpmyadmin

我试图调用存储过程getMarks()并在我的php页面上显示它。该代码在phpmyadmin中运行良好。但是,当我试图在我的php页面上显示它时,它会调用未定义的索引:student_name。

这是我的存储过程getMarks():

BEGIN

    SET group_concat_max_len=5058;
    SET @condition = 'MAX(CASE WHEN assessment_type = ''@at'' AND assessmet_no = @an THEN assessment_marks END) as @at@an';

    SELECT @conditions := GROUP_CONCAT(DISTINCT REPLACE(REPLACE(@condition, '@at', assessment_type), '@an', assessmet_no))
    FROM studentmarks;

    SET @sql = CONCAT('
    SELECT student_name, ', @conditions, ' 
    FROM studentmarks
    GROUP BY student_name');

    PREPARE stmt FROM @sql;
    EXECUTE stmt;


END

这是我的PHP代码:

$pstmt = 'call getMarks()';

$stmt = $dbh->prepare($pstmt);

$stmt->execute();
$item = $stmt->fetchAll(PDO::FETCH_ASSOC);

然后,我想在另一个php页面上显示这样的结果:

<?php foreach($item as $marks): ?>
<tr>
<td><?php echo $marks['student_name']; ?></td>
</tr>
<?php endforeach; ?>

不幸的是,错误未定义索引:student_name被调用。

如果我在phpmyadmin上执行它,我的程序结果如下:

+---------------+------------+----------+
|  student_name |  QUIZ1     |   QUIZ2  |
+---------------+------------+----------+
|  thaqif       |  4.00      |   5.oo   |
+---------------+------------+----------+
|  ajis         |  4.00      |   5.00   |
+---------------+------------+----------+

但是,当我尝试运行var_dump($item)时,结果就是这样。

array(1) { [0]=> array(1) { ["@conditions := GROUP_CONCAT(DISTINCT REPLACE(REPLACE(@condition, '@at', assessment_type), '@an', assessmet_no))"]=> string(287) "MAX(CASE WHEN assessment_type = 'QUIZ' AND assessmet_no = 1 THEN assessment_marks END) as QUIZ1,MAX(CASE WHEN assessment_type = 'QUIZ' AND assessmet_no = 2 THEN assessment_marks END) as QUIZ2,MAX(CASE WHEN assessment_type = 'TEST' AND assessmet_no = 0 THEN assessment_marks END) as TEST0" } } 

0 个答案:

没有答案