返还依于SQL查询变更的记录数
int mysql_affected_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_update = "UPDATE test_table SET age=age+1 WHERE age=10;";
$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_update); // send a query to update a record
if($result === false)
exit(mysql_error()); // print the error message and quit
else
{
$affected_rows = mysql_affected_rows($result); // get the number of affected rows
if($affected_rows > 0)
echo "$affected_rows record(s) have(has) been updated!";
}
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
?>