note 99159 modified in mysqli-stmt.store-result by danbrown

From: Date: Sun, 01 Aug 2010 14:07:57 +0000
Subject: note 99159 modified in mysqli-stmt.store-result by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171317@lists.php.net to get a copy of this message
When using prepare to prepare a statement to retrieve LOBs the method order matters. Also, method 'store_result()' must be called and be called in correct order. Failure to observe this causes PHP/MySQLi to crash or return an erroneous value. The proper procedure order is: prepare -> execute -> store_result -> bind -> fetch The following applies to a Windows SBS server running IIS/6.0 + PHP 5.2.1 MySQL server version 5.0.26-community-nt, client version 5.0.51a <?php $database = "test" ; $table = "test" ; $column = "flongblob" ; $mysqli = new mysqli("localhost", "root", "<secret_password>", $database); // Proper procedure order: prepare -> execute -> store_result -> bind -> fetch $stmt = $mysqli->prepare("SELECT $column FROM $table") ; $stmt->execute(); $stmt->store_result(); // Fetch a record. Bind the result to a variable called 'value' and fetch. $stmt->bind_result($value) ; $res = $stmt->fetch() ; if($res) { // strlen($value) should have LOB length, not 1 or zero. echo "$column data length is " . strlen($value) . " bytes.\n" ; } else { echo ((false !== $res) ? "End of data" : $stmt->error) . "\n" ; break ; } // Fetch another record. $res = $stmt->fetch() ; if($res) { // strlen($value) should have LOB length, not 1 or zero. echo "$column data length is " . strlen($value) . " bytes.\n" ; } else { echo ((false !== $res) ? "End of data" : $stmt->error) . "\n" ; break ; } $stmt->close() ; $mysqli->close() ; exit ; ?> The above example should output: flongblob data length is 932353 bytes. flongblob data length is 867300 bytes. If wrong procedure order MySQLi crashes or outputs: flongblob data length is 0 bytes. flongblob data length is 867300 bytes. --was-- When using prepare to prepare a statement to retrieve LOBs the method order matters. Also, method 'store_result()' must be called and be called in correct order. Failure to observe this causes PHP/MySQLi to crash or return an erroneous value. The proper procedure order is: prepare -> execute -> store_result -> bind -> fetch The following applies to a Windows SBS server running IIS/6.0 + PHP 5.2.1 MySQL server version 5.0.26-community-nt, client version 5.0.51a $database = "test" ; $table = "test" ; $column = "flongblob" ; $mysqli = new mysqli("localhost", "root", "<secret_password>", $database); // Proper procedure order: prepare -> execute -> store_result -> bind -> fetch $stmt = $mysqli->prepare("SELECT $column FROM $table") ; $stmt->execute(); $stmt->store_result(); // Fetch a record. Bind the result to a variable called 'value' and fetch. $stmt->bind_result($value) ; $res = $stmt->fetch() ; if($res) { // strlen($value) should have LOB length, not 1 or zero. echo "$column data length is " . strlen($value) . " bytes.\n" ; } else { echo ((false !== $res) ? "End of data" : $stmt->error) . "\n" ; break ; } // Fetch another record. $res = $stmt->fetch() ; if($res) { // strlen($value) should have LOB length, not 1 or zero. echo "$column data length is " . strlen($value) . " bytes.\n" ; } else { echo ((false !== $res) ? "End of data" : $stmt->error) . "\n" ; break ; } $stmt->close() ; $mysqli->close() ; exit ; The above example should output: flongblob data length is 932353 bytes. flongblob data length is 867300 bytes. If wrong procedure order MySQLi crashes or outputs: flongblob data length is 0 bytes. flongblob data length is 867300 bytes. http://php.net/manual/en/mysqli-stmt.store-result.php

« previous php.notes (#171317) next »