@@ -283,6 +283,23 @@ public function getOne($tableName, $columns = '*')
283283 return null ;
284284 }
285285
286+ /**
287+ * A convenient SELECT * function to get one value.
288+ *
289+ * @param string $tableName The name of the database table to work with.
290+ *
291+ * @return array Contains the returned column from the select query.
292+ */
293+ public function getValue ($ tableName , $ column )
294+ {
295+ $ res = $ this ->get ($ tableName , 1 , "{$ column } as retval " );
296+
297+ if (isset ($ res [0 ]["retval " ]))
298+ return $ res [0 ]["retval " ];
299+
300+ return null ;
301+ }
302+
286303 /**
287304 *
288305 * @param <string $tableName The name of the table.
@@ -300,8 +317,15 @@ public function insert($tableName, $insertData)
300317 $ stmt ->execute ();
301318 $ this ->_stmtError = $ stmt ->error ;
302319 $ this ->reset ();
320+ $ this ->count = $ stmt ->affected_rows ;
321+
322+ if ($ stmt ->affected_rows < 1 )
323+ return false ;
303324
304- return ($ stmt ->affected_rows > 0 ? $ stmt ->insert_id : false );
325+ if ($ stmt ->insert_id > 0 )
326+ return $ stmt ->insert_id ;
327+
328+ return true ;
305329 }
306330
307331 /**
@@ -422,7 +446,7 @@ public function join($joinTable, $joinCondition, $joinType = '')
422446 *
423447 * @return MysqliDb
424448 */
425- public function orderBy ($ orderByField , $ orderbyDirection = "DESC " )
449+ public function orderBy ($ orderByField , $ orderbyDirection = "DESC " , $ customFields = null )
426450 {
427451 $ allowedDirection = Array ("ASC " , "DESC " );
428452 $ orderbyDirection = strtoupper (trim ($ orderbyDirection ));
@@ -431,6 +455,13 @@ public function orderBy($orderByField, $orderbyDirection = "DESC")
431455 if (empty ($ orderbyDirection ) || !in_array ($ orderbyDirection , $ allowedDirection ))
432456 die ('Wrong order direction: ' .$ orderbyDirection );
433457
458+ if (is_array ($ customFields )) {
459+ foreach ($ customFields as $ key => $ value )
460+ $ customFields [$ key ] = preg_replace ("/[^-a-z0-9\.\(\),_]+/i " ,'' , $ value );
461+
462+ $ orderByField = 'FIELD ( ' . $ orderByField . ', " ' . implode ('"," ' , $ customFields ) . '") ' ;
463+ }
464+
434465 $ this ->_orderBy [$ orderByField ] = $ orderbyDirection ;
435466 return $ this ;
436467 }
@@ -627,6 +658,7 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
627658
628659 call_user_func_array (array ($ stmt , 'bind_result ' ), $ parameters );
629660
661+ $ this ->count = 0 ;
630662 while ($ stmt ->fetch ()) {
631663 $ x = array ();
632664 foreach ($ row as $ key => $ val ) {
@@ -794,8 +826,12 @@ protected function _buildOrderBy () {
794826 return ;
795827
796828 $ this ->_query .= " ORDER BY " ;
797- foreach ($ this ->_orderBy as $ prop => $ value )
798- $ this ->_query .= $ prop . " " . $ value . ", " ;
829+ foreach ($ this ->_orderBy as $ prop => $ value ) {
830+ if (strtolower (str_replace (" " , "" , $ prop )) == 'rand() ' )
831+ $ this ->_query .= "rand(), " ;
832+ else
833+ $ this ->_query .= $ prop . " " . $ value . ", " ;
834+ }
799835
800836 $ this ->_query = rtrim ($ this ->_query , ', ' ) . " " ;
801837 }
0 commit comments