@@ -326,6 +326,49 @@ public function rawQuery ($query, $bindParams = null)
326326 return $ res ;
327327 }
328328
329+ /**
330+ * Helper function to execute raw SQL query and return only 1 row of results.
331+ * Note that function do not add 'limit 1' to the query by itself
332+ * Same idea as getOne()
333+ *
334+ * @param string $query User-provided query to execute.
335+ * @param array $bindParams Variables array to bind to the SQL statement.
336+ *
337+ * @return array Contains the returned row from the query.
338+ */
339+ public function rawQueryOne ($ query , $ bindParams = null ) {
340+ $ res = $ this ->rawQuery ($ query , $ bindParams );
341+ if (is_array ($ res ) && isset ($ res [0 ]))
342+ return $ res [0 ];
343+
344+ return null ;
345+ }
346+
347+ /**
348+ * Helper function to execute raw SQL query and return only 1 column of results.
349+ * If 'limit 1' will be found, then string will be returned instead of array
350+ * Same idea as getValue()
351+ *
352+ * @param string $query User-provided query to execute.
353+ * @param array $bindParams Variables array to bind to the SQL statement.
354+ *
355+ * @return mixed Contains the returned rows from the query.
356+ */
357+ public function rawQueryValue ($ query , $ bindParams = null ) {
358+ $ res = $ this ->rawQuery ($ query , $ bindParams );
359+ if (!$ res )
360+ return null ;
361+
362+ $ limit = preg_match ('/limit\s+1;?$/i ' , $ query );
363+ $ key = key ($ res [0 ]);
364+ if (isset ($ res [0 ][$ key ]) && $ limit == true )
365+ return $ res [0 ][$ key ];
366+
367+ $ newRes = Array ();
368+ for ($ i = 0 ; $ i < $ this ->count ; $ i ++)
369+ $ newRes [] = $ res [$ i ][$ key ];
370+ return $ newRes ;
371+ }
329372 /**
330373 *
331374 * @param string $query Contains a user-provided select query.
0 commit comments