mysql_result()


返还特定数据表行和列的结果记录

Description

string mysql_result(string $result, int $row [, int $col = 0])

Parameters

Return Value

成功时相应数据值,失败时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
{
    $value = mysql_result($result, 1, 1);        // get a data in 1st row in 1st column
    if($value)
        echo "$value\r\n";                       // print the data
    else
        echo "No data 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
?>

See also