php - Use bind_result & fetch () or store_result() instead get_result -


how change function function. don't want use get_result

i searched online not find answer me.

public function select($table_name, $conditions='' ,$array_conditions_limit=null , $orderby='', $limit='', $selected_fields='*') {     $query = "select ".$selected_fields." ".$table_name;     if(!empty($conditions))         $query .= " ".$conditions;     if(!empty($orderby))         $query .= " order ".$orderby;     if(!empty($limit))         $query .= " limit ".$limit;      $statment = $this->connectionresult->prepare($query);     if(isset($array_conditions_limit)  )      {         $statment = $this->dynamicbindvariables($statment, $array_conditions_limit);         $statment->execute();         return $statment->get_result();      }      else         $statment->execute();         return $statment->get_result(); } 

this functions dynamic bind variables

private function dynamicbindvariables($statment, $params) {     if (is_array($params) && $params != null)     {         // generate type string (eg: 'issisd')         $types = '';         foreach($params $param)         {             $types .= $this->gettype($param);         }         // add type string first parameter         $bind_names[] = $types;          // loop thru given parameters         ($i=0; $i<count($params);$i++)         {             $bind_name = 'bind' . $i;             // add parameter variable              $$bind_name = $params[$i];             // associate variable element in array             $bind_names[] = &$$bind_name;         }         // call function bind_param dynamic parameters         call_user_func_array(array($statment,'bind_param'), $bind_names);     }     elseif(isset($params) && !empty($params))     {         $types = '';         $types .= $this->gettype($params);         $statment->bind_param($types ,$params);     }     return $statment; } 

i using return value follows:

$myresult =select('post','post_category=?' ,2  );                 $row = $myresul2->fetch_object() 

first of all, find approach utterly useless. doing dismembering fine sql sentence anonymous parts.

"select * post post_category=?" 

looks way better anonymous parameters of noone have idea.

'post','post_category=?' 

one can tell @ glance first statement do. , have no idea on second. not mention it's extreme:

'post','post_category=?',null, null, 'username, password' 

so, instead of kindergarten query builder rather suggest function accepts 2 parameters - query , array bound data:

$myresult = select("select * post post_category=?", [2]); 

to make more useful, wouild make separate functions different result types, making second line fetch_object() obsolete (however, speaking of objects, totally useless represent table row). example:

$row = $db->selectrow("select * post post_category=?", [2]); 

look: it's concise yet readable!

as further step may wish implement more placeholder types, allow fields order clause parameterized well:

$data = $db->getall('id','select * t id in (?a) order ?n', [1,2],'f'); 

you can see how works, other functions , use cases in safemysql library


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -