致命错误:在非对象上调用成员函数setFetchMode()

时间:2013-11-14 10:41:40

标签: php mysql

我在PHP中收到此错误:

Fatal error: Call to a member function setFetchMode() on a non-object

源代码:

<?php
$c1=103;//customer ID
$c2=104;//customer ID   
try {
$conn = new PDO("mysql:host=localhost;dbname=stored_procedure_examples","root","");
// execute the stored procedure
$sql = 'CALL GetCredit($c1,$c2)';//passing customer ID 103 and 104 as parameter
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table>
<tr>
<th>Credit Limit</th>
</tr>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['creditlimit'] ?></td>
</tr>
<?php endwhile; ?>
</table>

MySQL存储过程:

DELIMITER $$
CREATE PROCEDURE GetCredits(in p_cusno1 int(11),p_cusno2 int(11))
BEGIN
SELECT creditlimit  FROM customers WHERE customerNumber IN(p_cusno1,p_cusno2);
END$$

注意:如果我直接传递客户ID,则可以

如果我通过使用c1c2之类的变量间接传递,则会抛出错误

Fatal error: Call to a member function setFetchMode() on a non-object

1 个答案:

答案 0 :(得分:0)

通过双引号传递变量,因为单引号不能获得变量的值。

change:
$sql = 'CALL GetCredit($c1,$c2)';

to:
$sql = "CALL GetCredit($c1,$c2)";