从PDO转换为MySQLI
after a lot of hours troubleshooting i found that my hoster (hosting2go.nl) does not support PDO::mysql they just dont installd the driver so my Model.php in my MVC framework is screwed. I solved the problems with my database-conection. The problem is that i am completely new to mysqli and justr cant find out how to make my model.php (writen for PDO::mysql) writen for Mysqli (instead of PDO) i hope you guys can help me out... My code:
|ORIGINAL PDO MODEL.PHP|
class Model
{
function __construct()
{
$this->db = new Database;
$this->data = $_POST;
}
public function query( $data = array(), $query){
try{
$result = $this -> db -> prepare($query);
$result->execute($data);
$result = $result->fetchAll(PDO::FETCH_ASSOC);
return $result;
}catch(PDOException $e){
echo $e;
}
}
|My Database-connection|
class Database extends mysqli {
public function __construct() {
parent::__construct("localhost","root","","bartsite");
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
}
}
|This is what i made of my model.php FOR MYSQLI|
class Model
{
function __construct()
{
$this->db = new Database;
$this->data = $_POST;
}
public function query( $data = array(), $query){
try{
echo $query;
$result = $this -> db -> prepare($query);
$result = $this -> db -> query($data);
$result->fetch_all();
//$result->execute($data);
//$result = $result->fetchAll(PDO::FETCH_ASSOC);
}catch(PDOException $e){
echo $e;
}
}
}
|usefull part f the controller|
public function index(){
$data = $this->posts->getAllPosts();
var_dump($data);
$this->view->render("posts/index",$data);
return true;
}
|posts.models.php| class postsModel extends Model {
function __construct()
{
parent::__construct();
}
public function getAllPosts(){
$result = $this->query(
array(),
"SELECT * FROM posts ORDER BY id DESC"
);
return $result;
}
If you have in your mind to change hosting in future i suggest you to implement PDO interface by yourself. After your hoster add PDO or you change hoster you can switch to native PDO painless.
UPD: Pros: + you can test your own PDO implementation to be compatible with canonical PDO + so you can easy migrate
Cons: - a lot of work to do
Possible hack: implement PDO interface partly, only that stuff you really use
UPD-2 you can just get new hosting and forget about this one if it will be more cost-effective to you to do not mess with mysqli_* and other. Also it depends on your code base: there is no problem to change few methods or so, but not a huge cms or stuff like that. So the choice is up to you.
UPD-3
replace try-catch block in Model
with this code
echo $query;
$stmnt = $this -> db -> prepare($query);
// bind params to statement from $data here
if (false === $stmnt->execute()) {
// error
echo $e;
return;
}
$result = $stmt->get_result();
I can't find out how to make my model.php writen for Mysqli
As a matter of fact, you can't do it without a LOT of pain. Mysqli neither have bindValue, not it can return you an array of arbitrary structure.
Nevertheless, your Model class is wrong.
- first, for some reason it has $data as first parameter, making you to write it always.
- next, you are catching an exception to echo it out, which is a big no-no
- finally, I hope you are creating only one instance of Model class as well as one of whatever Database class.
So, let me suggest you safeMysql instead, which is built upon mysqli but being more user friendly and safe than PDO.
Say, for postsModel
class postsModel {
function __construct(safeMysql $db)
{
$this->db = $db;
}
public function getAllPosts(){
return $this->db->getAll("SELECT * FROM posts ORDER BY id DESC")
}
}
$db = new safeMysql();
$model = new postsModel($db);
$posts = $model->getAllPosts();