0

Good day. my problem is if i query from a table. i want to see the result of how many row has been retrieve. but it was displaying all the total rows in may table. i want the exactly number of the result.

<div id="lala">
<?php $query = "SELECT COUNT(*) AS total FROM tblreg where  status='reg'"; 
$result = mysql_query($query); 
$values = mysql_fetch_assoc($result); 
$num_rows = $values['total']; ?>

<h1>(<?php echo $num_rows ?>)&nbsp&nbspRegistered Member</h1>
</div>
<div id="formdesign"><input type="text" name="filter" value="" id="filter" placeholder="Search " autocomplete="off" />  </div>
<table id="resultTable" data-responsive="table">
<thead>
<tr>
<th>Username</th>
<th>Fullname</th>
<th>Course</th>
<th>Year Graduated</th>
<th>Email</th>
<th>Send Email</th>

<th>Delete</th>
</tr>
</thead>
        <?php

        include("dbcon.php");

        $result=mysql_query("SELECT * FROM tblreg where status = 'reg'");

        while($test = mysql_fetch_array($result))
        {

            $id = $test['reg_id'];  

            echo "<tr align='center'>";
            echo"<td>" .$test['username']."</td>";
            echo"<td>" .$test['fullname']."</td>";
            echo"<td>" .$test['course']."</td>";
            echo"<td>" .$test['year_grad']."</td>";
            echo"<td>" .$test['email']."</td>";
            echo"<td> <a href='email.php?id=$id' rel='facebox[.bolder]' ><img src='icons/e_mail.png'></a>";

            echo"<td> <a href='deleteregmember.php?id=$id' onclick='return confirm_delete()'><img src='icons/list-error.png'></a>";

            echo "</tr>";
        }
        mysql_close();
        ?>
        </tr>

  • 1
    Isn't there a MySQL function for: `mysql_num_rows($result)`. On a side note: STOP using MySQL PHP functions they're deprecated and will be removed in the future. Use MySQLi or PDO. – Mouser Feb 07 '15 at 14:03
  • Sir can you help me how to solve this? – Ian Navarro Feb 07 '15 at 14:07

3 Answers3

0
<h1>(<?php echo mysql_num_rows($result); ?>)&nbsp&nbspRegistered Member</h1>

That should display the amount of rows fetched from the database.

I you want to use PDO please read this: How to replace MySQL functions with PDO?

Community
  • 1
  • 1
Mouser
  • 13,132
  • 3
  • 28
  • 54
0

You can't access the database with Client-Side JavaScript.

With PHP you can access it by inserting this line after $result=mysql_query("SELECT * FROM tblreg where status = 'reg'");:

echo mysql_num_rows($result);

This way you will get the rows affected by the query.

On the other hand, use mysqli_* or PDO functions, since mysql_* functions are outdated and depracted.

You can find the official documentations on PHP's site:

mysql_num_rows: http://php.net/manual/en/function.mysql-num-rows.php

mysqli_* functions: http://php.net/manual/en/book.mysqli.php

PDO : http://php.net/manual/en/book.pdo.php

UPDATE: In your code just make the $num_rows variable equal to mysql_num_rows($result);

So your code will look something like this:

<?php $query = "SELECT COUNT(*) AS total FROM tblreg where status='reg'"; 
$result = mysql_query($query); 
$values = mysql_fetch_assoc($result); 
$num_rows = mysql_num_rows($result); ?>   
Laszlo L. Mári
  • 186
  • 2
  • 12
0
echo mysql_num_rows($result);

http://php.net/manual/en/function.mysql-num-rows.php

sinaza
  • 820
  • 6
  • 19