mysql_num_rows()


Получение количества строк в качестве результата

Описание

int mysql_num_rows(string $result)

Параметры

Возврат значений

Возврат количества строк в случае успеха, в противном случае возврат -1

Пример

<?php
include_once "/lib/sn_dns.php";                  // include DNS library
include_once "/lib/sn_mysql.php";

$db_name = "testdb";                             // database's name to select

// query strings creating or deleting DB, inserting or updating records, and so on.
$query_create_db = "CREATE DATABASE testdb;";
$query_create_table = "CREATE TABLE test_table (id INTEGER NOT NULL, age INTEGER);";
$query_insert_1 = "INSERT INTO test_table (id, age) VALUES ('1', '10');";
$query_insert_2 = "INSERT INTO test_table (id, age) VALUES ('2', '10');";
$query_select = "SELECT * FROM test_table";
$query_drop_table = "DROP TABLE test_table;";
$query_drop_db = "DROP DATABASE testdb;";

$server_addr = "192.168.0.100";                  // IP address of MySQL server
$user_name = "user_id";                          // MySQL ID
$password = "password";                          // MySQL password

// connect to a MySQL server
if(mysql_connect($server_addr, $user_name, $password) === false)
    exit(mysql_error());                         // print the error message and quit

if(mysql_query($query_create_db) === false)      // send a query to make a database
    exit(mysql_error());                         // print the error message and quit

if(mysql_select_db($db_name) === false)          // select the database
    exit(mysql_error());                         // print the error message and quit

if(mysql_query($query_create_table) === false)   // send a query to make a table
    exit(mysql_error());                         // print the error message and quit

if(mysql_query($query_insert_1) === false)       // send a query to insert a record
    exit(mysql_error());                         // print the error message and quit

if(mysql_query($query_insert_2) === false)       // send a query to insert a record
    exit(mysql_error());                         // print the error message and quit

$result = mysql_query($query_select);            // send a query to select records
if($result === false)
    exit(mysql_error());                         // print the error message and quit
else
{
    $num_rows = mysql_num_rows($result);         // get the number of rows in result
    if($num_rows > 0)
        echo "$num_rows recoreds has been detected!\r\n";
}

if(mysql_query($query_drop_table) === false)     // send a query to delete the table
    exit(mysql_error());                         // print the error message and quit

if(mysql_query($query_drop_db) === false)        // send a query to delete the database
    exit(mysql_error());                         // print the error message and quit

mysql_close();                                   // disconnecting from the MySQL server
?>

Смотрите также