PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




mysql_affected_rows

Name

mysql_affected_rows — Reports the number of rows modified by the last MySQL query.

Synopsis

int mysql_affected_rows([connection]);
mysql link connection (optional): Connection handle returned by mysql_connect() or mysql_pconnect()

Returns

Integer; FALSE on error

Description

mysql_affected_rows() returns a count of the number of rows that were modified by the last MySQL query made using the specified connection . If the connection argument is not set, the last connection opened will be used.

If the specified query failed, mysql_affected_rows() returns -1. If an error occurs, FALSE is returned.

Version

PHP 3+, PHP 4+

See also

To find the number of rows returned by a query:

mysql_num_rows()



Example

Example 784. Find the number of rows affected by a query

<?php
// Included code that connects to a MySQL server and sets a default database
// See the MySQL Functions chapter introduction for the source code for the file
include ('mysql_connect.inc.php');

// Storing our query in a variable helps us debug more easily
$query = "INSERT INTO user VALUES ('chewbacca', password('Hragf!'))";

// Suppress errors with a single @ symbol
@ mysql_query ($query);

// Get the number of rows affected by the last query
$affected_rows = @ mysql_affected_rows ();

// Use the return value of mysql_affected_rows() to see if the previous query worked
if (-1 == $affected_rows) {
    die ("Query '$query' failed with error message: \"" . mysql_error () . '"');
}

echo ("Query '$query' affected '$affected_rows' row(s).");
?>

Example 785. Determine whether a REPLACE query replaced a row

<?php
// Included code that connects to a MySQL server and sets a default database
// See the MySQL Functions chapter introduction for the source code for the file
include ('mysql_connect.inc.php');

// Storing our query in a variable helps us debug more easily
$query = "REPLACE INTO user VALUES ('han', password ('JabbaSucksEggs'))";

// Suppress errors with a single @ symbol
@ mysql_query ($query);

// Get the number of rows affected by the last query
// Explicitly set the connection to use
$affected_rows = mysql_affected_rows ($mysql_link);

switch ($affected_rows) {
    case -1:
        die ("Query '$query' failed with error message: \"" . mysql_error () . '"');
        break;

    case 1:
        echo "Query '$query' did not replace an existing row.";
        break;

    case 2:
        echo "Query '$query' replaced an existing row.";
        break;

    default:
        echo "Something odd may have happened! Query '$query' affected '$affected_rows' rows.";
        break;
}
?>

Example 786. Find out how many values should be updated by an UPDATE query

<?php
// Included code that connects to a MySQL server and sets a default database
// See the MySQL Functions chapter introduction for the source code for the file
include ('mysql_connect.inc.php');

// Set an advisory lock called $table with a timeout of 5 seconds
mysql_query ("SELECT GET_LOCK('$table', 5)")
    or die ("Lock '$table' could not be acquired within 5 seconds.");

// Isolate our WHERE clause for easier use
$where_clause = "password = password('')";

// Find the login names of the users who set empty passwords
$query = "SELECT login FROM $table WHERE $where_clause";

$mysql_result = mysql_query ($query)
    or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');

// Find out how many passwords we should be changing
$expected = mysql_num_rows ($mysql_result);

// If we don't need to change any passwords, unlock the table, display a message, and exit
if (0 == $expected) {
    mysql_query ("SELECT RELEASE_LOCK('$table')");
    die ("No bad passwords were encountered!");
}

// Find out who was bad and used an empty password
// Change the bad user passwords to chunks of mangled data
$update = "UPDATE user SET password = password(md5(concat(rand(), login)))";

for ($row = 0; $row < $expected; ++$row) {
    $bad_users[] = mysql_result ($mysql_result, $row);

    $query = "$update WHERE login = '$bad_users[$row]'";

    mysql_query ($query)
        or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');
}

// Get the number of rows affected by the last query
$affected = mysql_affected_rows ();

// Unlock the table
mysql_query ("SELECT RELEASE_LOCK ('$table')");

// Check to see if everything worked
if ($expected == $affected) {
    echo "$affected empty user password(s) were replaced with chunks of mangled data.<br />"
        . 'The bad users were: <b>' . implode ('<br />', $bad_users) . '</b>';
} else {
    echo "Something went wrong.  We should have replaced $expected password(s), "
    . "but we only replaced $affected!";
}



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.