@@ -93,6 +93,67 @@ public function processQuery($sql, $type = NULL) {
9393 return $ data ;
9494 }
9595
96+ /**
97+ * Version of processQuery() that deals with prepared statements. As prepared
98+ * statements use variadic functions, much of this function's complexity
99+ * comes from wrapping a variadic function in a PHP 5.5 compatible way.
100+ *
101+ * @param string $query Same format as mysqli::prepare(), with usually one
102+ * or more "?" inside it.
103+ *
104+ * @param string $type Must be "num" or "assoc". Contrary to processQuery(),
105+ * it's not an optionnal argument. Due to this function being variadic.
106+ *
107+ * @param mixed ...$paramsToBind Same format as mysqli_stmt::bind_param()
108+ * It's a variadic function based on this PHP 5.5 compatible implementation
109+ * https://wiki.php.net/rfc/variadics#introduction
110+ * We will be able to simplify this once we require PHP 5.6
111+ * https://secure.php.net/manual/en/migration56.new-features.php#migration56.new-features.variadics
112+ */
113+ public function processPreparedQuery ($ query , $ type ) {
114+ $ paramsToBind = array_slice (func_get_args (), 2 ); // additional arguments
115+ // prepared statements specific code
116+ $ statement = $ this ->db ->prepare ($ query );
117+ $ this ->checkForError ();
118+ // The following is an implementation of the splat operator. This
119+ // will be simpler with PHP 5.6
120+ // https://secure.php.net/manual/en/migration56.new-features.php#migration56.new-features.splat
121+ // We need to pass references to bind_param(), hence the use of refValues()
122+ call_user_func_array ([$ statement , "bind_param " ], self ::refValues ($ paramsToBind )) ;
123+ $ statement ->execute ();
124+ $ result = $ statement ->get_result ();
125+
126+ // same as processQuery()
127+ $ this ->checkForError ();
128+ $ data = array ();
129+
130+ if ($ result instanceof mysqli_result) {
131+ $ resultType = MYSQLI_NUM ;
132+ if ($ type == 'assoc ' ) {
133+ $ resultType = MYSQLI_ASSOC ;
134+ }
135+ while ($ row = $ result ->fetch_array ($ resultType )) {
136+ if ($ this ->db ->affected_rows > 1 ) {
137+ array_push ($ data , $ row );
138+ } else {
139+ $ data = $ row ;
140+ }
141+ }
142+ $ result ->free ();
143+ } else if ($ result ) {
144+ $ data = $ this ->db ->insert_id ;
145+ }
146+
147+ return $ data ;
148+ }
149+
150+ private static function refValues ($ arr ){
151+ $ refs = array ();
152+ foreach ($ arr as $ key => $ value ) {
153+ $ refs [$ key ] = &$ arr [$ key ];
154+ }
155+ return $ refs ;
156+ }
96157}
97158
98159?>
0 commit comments