1

I have been trying to use the row count function which is part of the PDO's.. i am using the for each loop to go thru a list of broken parts and then count how many of each part i need to order but using the code i put bellow its not working and im recving this error

"Fatal error: Call to a member function rowCount() on a non-object in W:\xampp\htdocs\ICT_Devices\damage_log\damage_parts_find.php on line 15"

Any help would be awesome my php is located just bellow here and i know that the pdo connect file and also the for each loop work but with the row count side im having issues

<?php define('INCLUDE_CHECK',true); require '../lib/connect/PDO_connect.php'; foreach($db->query("SELECT * FROM `damage_list`") as $damage_part) { $parts=$damage_part['Damage']; $stmt = $db->query('SELECT * FROM damage_log where damage=$parts'); $row_count = $stmt->rowCount(); echo $parts. "=".$row_count; echo "<br>"; } ?> 
1
  • You should be preparing a parameterised query before the loop and binding $parts within the loop then executing the query. Also, if you're only after a rowcount, use SELECT COUNT(1) instead. It will perform better and be more compatible across database engines Commented Nov 28, 2012 at 3:25

2 Answers 2

1

Because when using single quote, variables will not be expanded.

So your query practically looks like:

SELECT * FROM damage_log where damage=$parts 

Thus an error occurs, and $stmt is FALSE.

Use double quote may fix your problem.

Also, since you're not quoting $parts in your SQL statement, make sure $parts is always a number or any other types that do not need quoting.

Sign up to request clarification or add additional context in comments.

Comments

0

The error message is telling you $stmt is not an object.

This is likely because your query is bad ... the $parts is not interpreted inside single quotes, and is thus in the query literally.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.