How to Display the Number of MySQL Table Rows (mysql_num_rows)

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn How to display the number of rows from the MySQL Table
  • You will learn How to use the mysql_num_rows function
  • You will learn How to Use the mysql_affected_rows Function

Requirements

  • You should be able to program basic php and HTML
  • You have to have text editor,media (CMD), browser, and web server to run it, then in this tutorial I use text editor that is (Notepad ++), and using chrome browser, and its web server is XAMPP Control Pane v3.2.2.

Difficulty

Either choose between the following options:

  • Basic

Tutorial Contents

In this PHP MySQL tutorial this time I will discuss 2 functions, PHP used to display the number of rows of MySQL Table. The functions we will discuss are mysql_num_rows and mysql_affected_rows.

  • How to use the mysql_num_rows function
  • explanation
    The mysql_num_rows () function is used to find out how many number of rows mysql_query function calls (). This function requires 1 argument, the resource variable resulted from the mysql_query () function.
  • examples of its use:

||

<?php //buat koneksi dengan MySQL $link=mysql_connect('localhost','root','');

//jika koneksi gagal, langsung get out from PHP
if (!$link)
{
die("Koneksi dengan MySQL gagal");
}

//gunakan database politeknik
$result=mysql_query('USE politeknik');
if (!$result)
{
die("Database mahasiswa gagal digunakan");
}
//jalankan query
$result=mysql_query("SELECT * FROM mahasiswa");
echo "Total mahasiswa berjumlah ".mysql_num_rows($result)." orang.";
?>
||

  • if the above code is executed, then the results obtained are as follows:
    | Total mahasiswa berjumlah 5 orang |
  • explanation
    The mysql_num_rows () function is suitable if we only want confirmation about whether or not a data is in the database.
  • How to Use the mysql_affected_rows Function ?
    The mysql_affected_rows () function is used to determine the number of rows of tables that are processed by MySQL queries. These results are usually necessary if we want to know in more detail about the ongoing process.

For example, we want to do a DELETE operation to a table. This can be done with the mysql_query function. But we can not know how many rows have been deleted.

If using MySQL, this information immediately after the query:
c2.jpg

  • Then To retrieve this information from PHP, I use the mysql_affected_rows () function. Here is an example of its use:
    ||
<?php //buat koneksi dengan MySQL $link=mysql_connect('localhost','root','');

//jika koneksi gagal, langsung get outi from PHP
if (!$link)
{
die("Koneksi dengan MySQL gagal");
}

//gunakan database politeknik
$result=mysql_query('USE politeknik');
if (!$result)
{
die("Database mahasiswa gagal digunakan");
}

//jalankan query
$result=mysql_query("DELETE FROM mahasiswa_ilkom WHERE umur='22'");
echo "Data yang di hapus sebanyak: ".mysql_affected_rows()." orang.";
?>
||
|Data yang di hapus sebanyak: 2 orang.
Both mysql_num_rows and mysql_affected_rows functions are often used in situations where we need information about MySQL queries that are executed. Both of these functions complement the other functions we have learned in this MySQL PHP tutorial

Curriculum

How to Split or Retrieve String using substr() php function



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

This tutorial is very basic and holds little to no value, anyone can find out how to use this function without the help of a tutorial.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.27
TRX 0.26
JST 0.039
BTC 94467.52
ETH 3348.45
USDT 1.00
SBD 3.35