I am migrating to PDO from mysql_select’s, I have the following code which works:
[php]
class DB{
private $un = ‘username’;
private $pw = ‘password’;
private $hst = ‘localhost’;
private $db = ‘test’;
private $PDO_;
private function DBcon(){
try {
$this->PDO_ = new PDO(‘mysql:host=’.$this->hst.’;dbname=’.$this->db.’’, $this->un, $this->pw);
$this->PDO_->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo 'ERROR: ’ . $e->getMessage();
}
return $this->PDO_;
}
[/php]
Now, for the past 2 hours I have tried to alter my old code which is another method within my database class. Can anyone give me an example of how to switch this over to PDO as well? I can do it outside of a class, but not within one. I am pretty new to OOP.
[php]
public function DBinsert($tbl,$val){
$sql=“insert into “.$tbl.” (”;
foreach($val as $name => $value){
$fieldsname[]=$name;
$fieldsval[]=$value;
}
$sqlfield =$fieldsname[0];
for($i=1;$i<count($fieldsname);$i++){
$sqlfield .=",".$fieldsname[$i];
}
$sqlval ="’".$fieldsval[0]."’";
for($i=1;$i<count($fieldsval);$i++){
$sqlval .=",’".$fieldsval[$i]."’";
}
$sql.=$sqlfield.") values (".$sqlval.")";
return mysql_query($sql) or die(mysql_error());
}
//End Class
}
[/php]