我得到这个错误在PHP上:致命错误:调用一个成员函数调用setFetchMode()非对象
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,它会起作用
如果我通过间接使用变量,如c1
和c2
它抛出错误
Fatal error: Call to a member function setFetchMode() on a non-object
请将您的查询放在双引号中。这可能会帮助你http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php –