note 99159 added to mysqli-stmt.store-result

From: Date: Fri, 30 Jul 2010 23:16:45 +0000
Subject: note 99159 added to mysqli-stmt.store-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-171277@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 $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. ---- Server IP: 192.41.42.26 Probable Submitter: 131.191.11.159 ---- Manual Page -- http://www.php.net/manual/en/mysqli-stmt.store-result.php Edit -- https://master.php.net/note/edit/99159 Del: integrated -- https://master.php.net/note/delete/99159/integrated Del: useless -- https://master.php.net/note/delete/99159/useless Del: bad code -- https://master.php.net/note/delete/99159/bad+code Del: spam -- https://master.php.net/note/delete/99159/spam Del: non-english -- https://master.php.net/note/delete/99159/non-english Del: in docs -- https://master.php.net/note/delete/99159/in+docs Del: other reasons-- https://master.php.net/note/delete/99159 Reject -- https://master.php.net/note/reject/99159 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#171277) next »