Good afternoon.

Task:

Having obtained a sample from the database in the usual way (SELECT * FROM table WHERE param=12) into the $result variable, you need to count the number of rows obtained from the database. Without *foreach*. count(array) *foreach*. count(array) - as you understand it does not consider such an array.

    2 answers 2

    Suppose we $query = mysql_query(...

    $num_rows = mysql_num_rows($query);

    echo $num_rows;

    In the new php7, all the same thing, only the functions are called mysqli_query and mysqli_num_rows, respectively.

       $mysqli_result = $mysqli->query(SELECT * FROM table WHERE param=12); print($mysqli_result->num_rows); 

      should help.

      • I'm afraid that the vehicle has not grown to mysqli) - Palmervan
      • I'm afraid if I write the solution again with mysql, @eicto will come again and hit on the header. What is the most offensive is rightly done - knes
      • then why not PDO? I think the answer should be clear to TS'u and not like @eicto: D) - Palmervan
      • Not PDO, because there is an ironic excuse: it is far from everywhere :) The answer should be clear to the vehicle, but it should not lead away from the first days of life. Many people still advise the construction of mysql_connect or die () on sites, although it is the simplest, it is far from the most correct. Even the same if (mysql_connect ()) {} else {show_page_try_again_later ()}; Many times better, but not as complicated as exams. - knes