我得到這個錯誤在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 –