0

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;
}
Bart Jan
  • 77
  • 8

2 Answers2

2

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();
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

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();
pinepain
  • 12,453
  • 3
  • 60
  • 65
  • yea but i already paid for a year,.. and it`s never wrong to learn something new. – Bart Jan Apr 29 '13 at 12:28
  • and this was my last hosting with this company. – Bart Jan Apr 29 '13 at 12:29
  • is there any refunds or something? – pinepain Apr 29 '13 at 12:29
  • 2
    it's just a 35 euro. not to much for the lesson that you have to check available extensions and other stuff first. i had such experience too, so i can recommend to forget about that bastard and do not mess with PDO implementation. Your time probably costs more. – pinepain Apr 29 '13 at 12:34
  • true that but i`m not planning to change the hosting. not the question ill just want to know how to adjust my model. – Bart Jan Apr 29 '13 at 12:37
  • ` }catch(PDOException $e){` <- this will not work without PDO installed. – pinepain Apr 29 '13 at 12:45
  • ok that worked ! but ill have the error:Warning: Missing argument 2 for Model::query(), called in /home/vhosts/broersma-state.nl/httpdocs/app/models/posts.model.php on line 18 and defined in /home/vhosts/broersma-state.nl/httpdocs/app/model.php on line 12 NULL NULL – Bart Jan Apr 29 '13 at 12:59
  • public function query( $data = array(), $query){ <- wrong, http://www.php.net/manual/en/functions.arguments.php#functions.arguments.default, try public function query( $data, $query){ – pinepain Apr 29 '13 at 13:01
  • probably you have to pass two arguments on /app/models/posts.model.php on line 18 – pinepain Apr 29 '13 at 13:09
  • 1
    @user2057856 as I told you in my answer, it won't work. You could scarcely make it work with empty data only. – Your Common Sense Apr 29 '13 at 13:11
  • my bad. mysqli::query doesn't accept args, so you have to bind args to query first – pinepain Apr 29 '13 at 13:16
  • ah that`s the lat prob i think – Bart Jan Apr 29 '13 at 13:18
  • i think if you are not proficient in software development (maybe you are but in other lang) the @YourCommonSense give you good example and maybe you should try his solution and even accept it if it will work for you – pinepain Apr 29 '13 at 13:23
  • i`m just starting in with programming and thanks you`ve helped me alot – Bart Jan Apr 29 '13 at 13:31