note 107348 added to mysqli.prepare

From: Date: Thu, 02 Feb 2012 06:38:39 +0000
Subject: note 107348 added to mysqli.prepare
Groups: php.notes 
Request: Send a blank email to php-notes+get-185827@lists.php.net to get a copy of this message
Prepared statements are confusing in the beginning .. mysqli->prepare() returns a so-called statement object which is used for subsequent operations eg execute, bind_param, store_result, bind_result, fetch, etc. The statement object has private properties which update as each statement operation is carried out. I found these useful for understanding what is going on when writing a prepared statement function: affected_rows insert_id num_rows param_count field_count errno error sqlstate id But it took a little time to get my head around accessing them: <?php $stmt = $mysqli->prepare($query); // .. $stmt-> operations .. var_dump($stmt); // shows null values var_dump($stmt->errno); // note literal, displays value // .. $stmt-> operations .. // to keep a copy .. // get_object_properties() won't work // clone() won't work $properties = array(); foreach ($stmt as $name => $priv){ $properties[$name] = $stmt->$name; // works // $properties[$name] = $priv; // won't work, foreach can't access private properties } $stmt->close(); // var_dump($stmt->errno) // won't work, $stmt is closed ?> ---- Server IP: 69.147.83.197 Probable Submitter: 165.228.140.126 ---- Manual Page -- http://www.php.net/manual/en/mysqli.prepare.php Edit -- https://master.php.net/note/edit/107348 Del: integrated -- https://master.php.net/note/delete/107348/integrated Del: useless -- https://master.php.net/note/delete/107348/useless Del: bad code -- https://master.php.net/note/delete/107348/bad+code Del: spam -- https://master.php.net/note/delete/107348/spam Del: non-english -- https://master.php.net/note/delete/107348/non-english Del: in docs -- https://master.php.net/note/delete/107348/in+docs Del: other reasons-- https://master.php.net/note/delete/107348 Reject -- https://master.php.net/note/reject/107348 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185827) next »