Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Connector
- {
- public $ok;
- public $db;
- public $prepared;
- public function __construct(){
- // db should be a reasonable mysqli instance.
- $this->db = new mysqli("host","usename","password","default schema",port,"");
- $this->prepared = array();
- }
- public function prepare($name, $sql, $types, $fetch="none", $firstonly=false){ // fetch:none/one/assoc/flat/onecol/id
- $this->prepared[$name] = array($this->db->prepare($sql),$types,$fetch,$firstonly,$sql);
- }
- public function execute($name,$data=array()){
- if(!is_array($data)){
- $data = array($data);
- }
- $this->ok=true;
- $stmt = $this->prepared[$name][0];
- $type = $this->prepared[$name][1];
- $ftch = $this->prepared[$name][2];
- $frst = $this->prepared[$name][3];
- if (!is_object($stmt)) {
- $this->ok=false;
- return -1;
- }
- call_user_func_array('mysqli_stmt_bind_param', array_merge (array($stmt, $type), $data));
- $stmt->execute();
- if($stmt->error!=""){
- $this->ok=false;
- return -1;
- }
- if($ftch=="one" || $ftch=="assoc" || $ftch=="flat" || $ftch=="onecol"){
- $meta = $stmt->result_metadata();
- while ($field = $meta->fetch_field()) {
- $params[] = &$row[$field->name];
- }
- call_user_func_array(array($stmt, 'bind_result'), $params);
- $empty=true;
- while ($stmt->fetch()) {
- $c=array();
- foreach($row as $key => $val) {
- $empty=false;
- if($ftch=="assoc"){
- $c[$key] = $val;
- }else{
- $c[] = $val;
- }
- if($ftch=="one"){
- $stmt->free_result();
- return $val;
- }
- }
- if($frst)
- return $c;
- if($ftch=="onecol")
- $hits[] = $c[0];
- else
- $hits[] = $c;
- }
- if($ftch=="id"){
- return $this->db->insert_id;
- }
- if($empty)
- return null;
- return $hits;
- $stmt->free_result();
- }
- return $this->db->insert_id;
- }
- public function describe(){
- $out = "";
- foreach($this->prepared as $key => $i){
- $out.= $key." = ".$i[4]." (".$i[1].") ";
- if($i[3]){
- if($i[2]=="flat"){
- $out.="flat value list for first row";
- }else if($i[2]=="assoc"){
- $out.="assoc key-value pairs for first row";
- }
- }else{
- if($i[2]=="flat"){
- $out.="lflat value list for each row";
- }else if($i[2]=="assoc"){
- $out.="assoc key-value pairs for each row";
- }else if($i[2]=="onecol"){
- $out.="a single value for each row";
- }else{
- $out.="no output";
- }
- }
- $out.="\n";
- }
- return $out;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment