-1

我在 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
4

1 回答 1

0

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

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

to:
$sql = "CALL GetCredit($c1,$c2)";
于 2013-11-14T10:46:39.783 回答