php - PDO Class and MYSQL request -


i have pdo class wrapper:

class db {          private $dbh;         private $stmt;          public function __construct($user, $pass, $dbname) {              $dsn = 'mysql:host=localhost;dbname=' . $dbname;              $options = array(                 pdo::mysql_attr_init_command => 'set names utf8',                 pdo::attr_persistent => true             );              try {                 $this->dbh = new pdo($dsn, $user, $pass, $options);             }             catch (pdoexception $e) {                 echo $e->getmessage();                 die();             }         }          public function query($query) {             $this->stmt = $this->dbh->prepare($query);             return $this;         }          public function bind($pos, $value, $type = null) {              if( is_null($type) ) {                 switch( true ) {                     case is_int($value):                         $type = pdo::param_int;                         break;                     case is_bool($value):                         $type = pdo::param_bool;                         break;                     case is_null($value):                         $type = pdo::param_null;                         break;                     default:                         $type = pdo::param_str;                 }             }               $this->stmt->bindvalue($pos, $value, $type);              return $this;         }          public function execute() {             $this->stmt->execute();         }          public function resultset() {             $this->execute();             return $this->stmt->fetchall(pdo::fetch_assoc);          }          public function single() {             $this->execute();             return $this->stmt->fetch();         }     } 

question 1: have request below:

$ids_set = implode(",", $ids); // return 2,4 $sql = 'select `id`, `qty` `products` `id` in (:products_ids) order `id`'; $arr = $this->db->query($sql)->bind(":products_ids", $ids_set)->resultset(); 

but request return 1 element in array:

array (     [0] => array         (             [id] => 2             [qty] => 1         )  ) 

but should return 2 elements. why , how modify class?

question 2

is class bind function safe db injections?

question 3

i have dynamic query:

$sql    = 'select count(*) `orders` 1=1';  if ($filter["order"] != 0) {     $sql .= ' , `gatewayid` = '.intval($filter["order"]).''; }  $count  = $this->db->query($sql)->single()[0]; 

how can use bind function in case? thanks!

frankly, find class quite useless at moment. can have same raw pdo:

$arr = $this->db->prepare($sql)->execute([":product_id" => $id])->fetchall(); 

speaking of binding complex data types - the sane solution to devise additional placeholder types. , support such placeholders real purpose of class.

question 1

you have create string placeholders dynamically, as explained in answer

question 2

yes

question 3

you have create query dynamically, array data bind
shown pdo class quite unreliable this:

$sql  = 'select count(*) `orders` 1=1'; $data = []; if ($filter["order"] != 0) {     $sql .= ' , `gatewayid` = :getewayid';     $data[":getewayid"] = $filter["order"]; } $count = $this->db->prepare($sql)->execute($data)->fetchcolumn(); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -