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

Php 致命错误:对非对象调用成员函数setFetchMode(),php,mysql,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

我在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>
注意:如果我直接传递客户ID,它就会工作

如果我通过使用像
c1
c2
这样的变量间接传递,则抛出错误

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

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

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

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

请把你的问题写在双引号里。这可能对你有帮助
change:
$sql = 'CALL GetCredit($c1,$c2)';

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