paddy_fields Posted December 5, 2013 Share Posted December 5, 2013 (edited) Hi. I was using the method below to count the rows in a table, but I've since learnt I should be using SQL Count. Old method.... //total number of jobs if ($result = $db->query("SELECT * FROM jobBoard")) { $totalJobCount = $result->num_rows; $result->close(); echo $totalJobCount; } I've now changed this to.... //total number of jobs if ($result = $db->query("SELECT count(*) AS jobCount FROM jobBoard")) { $findCount = $result->fetch_assoc(); $totalJobCount = $findCount['jobCount']; $result->close(); echo $totalJobCount; } Is my new method the best way of doing this? It provides the correct result but I'm new to mysqli and I'm not sure if fetch_assoc() is the right way to go? Thanks. Pat. Edited December 5, 2013 by paddyfields Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted December 5, 2013 Solution Share Posted December 5, 2013 you can shorten - $findCount = $result->fetch_assoc(); $totalJobCount = $findCount['jobCount']; to be - list($totalJobCount) = $result->fetch_row(); Quote Link to comment Share on other sites More sharing options...
paddy_fields Posted December 5, 2013 Author Share Posted December 5, 2013 That's great, thank you. Just to get my head around that... is $result an array with one value? And then list($totalJobCount) assigns that array item to $totalJobCount? Quote Link to comment Share on other sites More sharing options...
Barand Posted December 5, 2013 Share Posted December 5, 2013 $result->fetch_row() will return an array with 1 item (the count) Quote Link to comment Share on other sites More sharing options...
paddy_fields Posted December 5, 2013 Author Share Posted December 5, 2013 Ok thanks, I understand. Good to know! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.