-1

I am getting this error in PHP:

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

Source code:

<?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 Stored Procedure:

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$$

Note: if I pass the customer ID directly it works

if I pass indirectly by using variables like c1 and c2 it's throwing error

Fatal error: Call to a member function setFetchMode() on a non-object
glglgl
  • 89,107
  • 13
  • 149
  • 217
Ramkumar
  • 11
  • 2
  • please put your query in double quotation. and this might help you http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – Bhavik Patel Nov 14 '13 at 10:50

1 Answers1

0

pass the variables through double quotes, because single quotes don't get the value of variables.

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

to:
$sql = "CALL GetCredit($c1,$c2)";
rray
  • 2,518
  • 1
  • 28
  • 38