dtr87341 2013-04-29 12:01
浏览 83
已采纳

从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;
}
  • 写回答

2条回答 默认 最新

  • dongxian1699 2013-04-29 12:24
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退