Skip to content

Instantly share code, notes, and snippets.

@Anan5a
Last active May 30, 2016 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anan5a/c004db57ce93f09c7754957c5f8f1f5d to your computer and use it in GitHub Desktop.
Save Anan5a/c004db57ce93f09c7754957c5f8f1f5d to your computer and use it in GitHub Desktop.
<?php
class W_PDO_DB{
private $type,$host,$user,$pwd,$db;
protected $dbh;
function __construct($type,$host,$user,$pwd,$db) {
$this->type =$type;
$this->host =$host;
$this->user =$user;
$this->pwd =$pwd;
$this->db =$db;
$dsn="$type:host=$host;dbname=$db";
try {
$this->dbh= new PDO($dsn,$user,$pwd);
}
catch (PDOException $e) {
echo "Something went wrong :(
<br>Class : <i>".__CLASS__."</i>
<br>file : <b>".__FILE__."</b>
<br>ERROR : ". $e->getMessage()."<br><br>";
}
}//end conn
function insert($table,$data=array()){
$keys=join(',',array_keys($data));
$what=trim(str_repeat('?,',count(array_values($data))),',');
$value=array_values($data);
$query="INSERT INTO $table($keys) VALUES ($what)";
$sth = $this->dbh->prepare($query);
$i=0;
while($i<=count($data)/2){
foreach($value as $val){
$i++;
$sth->bindParam($i,$val);
}
}
$sth->execute();
}//end insert
}
$h=new W_PDO_DB('mysql','127.0.0.1','root','','test');
$h->insert('X',array('name'=>'Ananta 7','phone'=>'1234'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment