mysql - PHP Counting Number of Rows -
i have following code used echo number of columns accident , wanted count rows i've changed not echoing anything. ideas? way have connected database though not shown.
<p> <?php $result = mysqli_query("select * user_info"); $num_rows = mysqli_num_rows($result); echo $num_rows[0]; mysqli_close($con); ?> records in our database start search <a href="#">here</a> today. </p>
as general rule, should minimize traffic between db server , application server.
for specific case, means recommended way of "counting" is:
<?php $result = mysqli_query("select count(*) user_info"); $row = mysqli_fetch_row($result); $num = $row[0]; ?>
with request (select count(*) ...
), db server count rows you. , return total -- possibly using indexes or internal data structure return directly result. select *
, server has retrieve , send data in table. quite expensive , rather inefficient since don't use these data after in application...
edit -- if want "simple" explanation: efficient know number of pages in book? count them 1 one, or @ page numbers in footer?
with initial solution (counting in php), making xerox of book, , counting pages in copy. count(*)
let db server use page numbers or index (the "toc" of book) find result.
Comments
Post a Comment