PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
include 'db.php';
$sql = "SELECT COUNT(*) AS numrows FROM px_topics WHERE parent_id = '1";
$queryResource = mysql_query($sql);
$row = mysql_fetch_array($queryResource);
echo $rowї'numrows'] . " rows selected<br />";
I am trying to count all the rows where the parent_id is 1 and show how many rows containing 1 there are but the script above produces the error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\Moi.PAUL\Desktop\www\project\listings\New Folder\final\head.php on line 24
Speed is not everything, and dependant on what you are trying to do and your app may determine what the best approach would be....
If you also want to use the information (e.g. you want to query the database to find all users called 'Adam', give a total then display. Then the best approach would be to query the database and use num_rows for the total (or alternatively, query the database, retrieve all results into an array and count the size of the array).
If you just need a count, but you also have many other queries running that may return large amounts of data then SELECT COUNT would be the way to go. Take for example you had a query that returned 3000 rows but you only need the count, if you issue a standard query then you have 3000 rows sitting in memory. If you issue a SELECT COUNT then you only have one row. As I very rarely see anyone issuing a free_result in the code they post on these boards you could quite easily run out of memory or bog your server down if you have a large scale app serving up multiple requests 24/7.
While speed may not be of any major concern for small scripts, it can be helpful to to identify and remove potential bottlenecks in any script/app. I have yet to meet a coder who would not implement the best performing solution even if the gains were relatively small.
we know redmonkey, i was just saying that its fine for ek5932's prob, cos he is limiting it with id, so it wont matter much unless there are other queries in the page.