downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mysqli_stmt::attr_get> <MySQLi_STMT
Last updated: Fri, 13 Nov 2009

view this page in

mysqli_stmt->affected_rows

mysqli_stmt_affected_rows

(PHP 5)

mysqli_stmt->affected_rows -- mysqli_stmt_affected_rows直近に実行されたステートメントで変更・削除・あるいは追加された行の総数を返す

説明

オブジェクト指向型(プロパティ):

int $affected_rows;

手続き型 :

int mysqli_stmt_affected_rows ( mysqli_stmt $stmt )

INSERTUPDATE あるいは DELETE クエリによって変更された行の数を返します。

この関数は、テーブルを更新するクエリに対してのみ働きます。 SELECT クエリが返す行の数を知るには、 mysqli_stmt_num_rows() 関数を代わりに使用します。

パラメータ

stmt

手続き型のみ: mysqli_stmt_init() が返すステートメント ID。

返り値

ゼロより大きい整数の場合、変更した行の数を示します。ゼロの場合は、 UPDATE/DELETE で 1 行も更新されなかった・WHERE 句にマッチする行がなかった・ あるいはまだクエリが実行されていないのいずれかであることを示します。 -1 は、クエリがエラーを返したことを示します。 NULL は、関数に無効な引数が渡されたことを示します。

注意: 変更された行の数が PHP の int 型の最大値をこえる場合は、変更された 行の数は文字列として返されます。

例1 オブジェクト指向型

<?php
$mysqli 
= new mysqli("localhost""my_user""my_password""world");

/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

/* 一時テーブルを作成します */
$mysqli->query("CREATE TEMPORARY TABLE myCountry LIKE Country");

$query "INSERT INTO myCountry SELECT * FROM Country WHERE Code LIKE ?";

/* ステートメントを準備します */
if ($stmt $mysqli->prepare($query)) {

    
/* プレースホルダに変数をバインドします */
    
$code 'A%';
    
$stmt->bind_param("s"$code);

    
/* ステートメントを実行します */
    
$stmt->execute();

    
printf("rows inserted: %d\n"$stmt->affected_rows);

    
/* ステートメントを閉じます */
    
$stmt->close();
}

/* 接続を閉じます */
$mysqli->close();
?>

例2 手続き型

<?php
$link 
mysqli_connect("localhost""my_user""my_password""world");

/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

/* 一時テーブルを作成します */
mysqli_query($link"CREATE TEMPORARY TABLE myCountry LIKE Country");

$query "INSERT INTO myCountry SELECT * FROM Country WHERE Code LIKE ?";

/* ステートメントを準備します */
if ($stmt mysqli_prepare($link$query)) {

    
/* プレースホルダに変数をバインドします */
    
$code 'A%';
    
mysqli_stmt_bind_param($stmt"s"$code);

    
/* ステートメントを実行します */
    
mysqli_stmt_execute($stmt);

    
printf("rows inserted: %d\n"mysqli_stmt_affected_rows($stmt));

    
/* ステートメントを閉じます */
    
mysqli_stmt_close($stmt);
}

/* 接続を閉じます */
mysqli_close($link);
?>

上の例の出力は以下となります。

rows inserted: 17

参考



mysqli_stmt::attr_get> <MySQLi_STMT
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
mysqli_stmt->affected_rows
Senthryl
03-Mar-2009 07:11
This property contains a -1 value in situations when no errors have occurred.  When preparing a SELECT statement (or any other statement which doesn't affect rows), the property will be -1.  The property will also be reset to -1 if an INSERT statement is re-prepared as a SELECT statement.

For example:
<?php
    $mysqli
= new mysqli('localhost', 'user', 'pass', 'test');
   
$stmt = $mysqli->stmt_init();
   
$param = 'value';

   
//First SELECT
   
$stmt->prepare('SELECT * FROM `test` WHERE `field`=?');
   
$stmt->bind_param('s', $param);
   
var_dump($stmt->execute());
   
var_dump($stmt->affected_rows);
    echo
'<br>';

   
//INSERT
   
$stmt->prepare('INSERT INTO `test` (`field`) VALUES (?)');
   
$stmt->bind_param('s', $param);
   
var_dump($stmt->execute());
   
var_dump($stmt->affected_rows);
    echo
'<br>';

   
//Second SELECT
   
$stmt->prepare('SELECT * FROM `test` WHERE `field`=?');
   
$stmt->bind_param('s', $param);
   
var_dump($stmt->execute());
   
var_dump($stmt->affected_rows);
    echo
'<br>';
?>

Displays:
bool(true) int(-1)
bool(true) int(1)
bool(true) int(-1)
Chuck
29-Apr-2007 05:51
I'm not sure whether or not this is the intended behavior, but I noticed through testing that if you were to use transactions and prepared statements together and you added a single record to a database using a prepared statement, but later rolled it back, mysqli_stmt_affected_rows will still return 1.
Carl Olsen
27-Dec-2005 06:34
It appears that an UPDATE prepared statement which contains the same data as that already in the database returns 0 for affected_rows.  I was expecting it to return 1, but it must be comparing the input values with the existing values and determining that no UPDATE has occurred.

mysqli_stmt::attr_get> <MySQLi_STMT
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites