Home php Find out the number of entries in the MYSQL Table

Find out the number of entries in the MYSQL Table

Author

Date

Category

You need to find out how many users in the user users in which the value rating is more than 10.
Please tell me how to do it?


Answer 1, Authority 100%

If you need to know only the number of rows, but the rows themselves do not need

To find out the number of entries in the table, you need to use SQL function COUNT () :

select count (*) from users

If you need to calculate the number of records by condition, it must be added to the request:

select count (*) from users where Rating & gt; ten

To get this value in the PNP, you can request the usual order

$ res = $ Conn- & gt; query ("Select Count (*) from Users WHERE RATING & GT; 10");
$ row = $ res- & gt; fetch_row ();
$ Count = $ Row [0];

If the condition is set dynamically, then the prepared expressions are required when requesting:

$ rating = 10;
$ STMT = $ Conn- & GT; Prepare ("SELECT COUNT (*) from Users Where Rating & GT;?");
$ STMT- & gt; bind_param ("s", $ rating);
$ STMT- & gt; execute ();
$ res = $ STMT- & GT; Get_Result ();
$ row = $ res- & gt; fetch_row ();
$ Count = $ Row [0];

If you need to get the lines themselves, and also find out how much they are

is even easier here. Just get all lines in an array:

$ res = $ Conn- & gt; query ("Select * from Users WHERE RATING & GT; 10");
$ rows = $ res- & gt; fetch_all (mysqli_assoc);

Here we received a conventional array, with which we can do the same as with any other array. For example, if we need to know if there is anything in the array, we can substitute it in directly in the condition:

if (! $ rows) {
  Echo "Nothing found";
} else {
  Foreach ($ Rows AS $ Row) {
    Echo $ Row ['Name'];
  }
}

If you suddenly need to get exactly the number of rows, then simply perform the usual count () function

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions