mysql_fetch_row()


getting a result row as an enumerated array

Description

array mysql_fetch_row(string $result)

Parameters

Return Value

an array string on success, otherwise false

Example

<?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
{
    while(1)
    {
        $arr = mysql_fetch_row($result);         // get a record
        if($arr === false)
          break;
        $arr_len = count($arr);                  // the number of column
        for($i = 0; $i < $arr_len; $i++)
        {
          if($arr[$i] !== "")
            printf("%s ", $arr[$i]);             // print a data
          else
            break;
        }
        echo "\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
?>

See also