2

I'm creating some basic OO scripts using MySQLi and getting an Undefined Method error when I use any of stmt_init(), prepare() or query() ... Also getting an error with connect_errno(). I DO know that mysqli extension is enabled (uncommented) in my php.ini and phpinfo() has both mysqli and mysqlnd enabled ... So not sure why I can't access the methods/properties. The error I'm getting is: Fatal error: Call to undefined method mysqli::connect_error()

class db {
    public $host = 'localhost';
    public $username = 'root';
    public $password = '';
    public $database = 'molecule';
    public $mysqli = '';
    function __construct() {
        $this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
    return $this->mysqli;
    }   
}

class nodeModel {
    function __construct() {
        $this->mysqli = new db;
        if($this->mysqli->connect_error()){ printf("Database Connection failed: %s\n", $this->mysqli->connect_error()); }
    }

    function insertNode() {
        $this->insert = $this->mysqli->stmt_init();
        $this->insert->prepare("INSERT INTO node(node_name, node_link, node_comment) VALUES (?, ?, ?)");
        $this->insert->bind_param($this->node_name, $this->node_link, $this->node_comment);
        if($this->insert->execute()) {
            $this->insert_id = $this->mysqli->insert_id;
        }
        $this->insert->close();
        print_r($this->insert_id);
        return $this->insert_id;
    }

So, to get the insertNode() method to work ... these are the other methods I put in the nodeModel class

public function setNodeName($value) { $this->nodeName = $value; }
public function setNodeLink($value) { $this->nodeLink = $value; }
public function setNodeComment($value) { $this->nodeComment = $value; }
public function getNodeName() { return $this->nodeName; }
public function getNodeLink() { return $this->nodeLink; }
public function getNodeComment() { return $this->nodeComment; }

public $insert_id;
function insertNode($nodeName, $nodeLink, $nodeComment) {
        $this->mysqli->stmt_init();
        $this->mysqli->prepare("INSERT INTO node(node_name, node_link, node_comment) VALUES (?, ?, ?)");
        $this->mysqli->bind_param($this->node_name, $this->node_link, $this->node_comment);
        if($this->mysqli->execute()) {
            $this->insert_id = $this->mysqli->insert_id;
        }
        $this->mysqli->close();
        print_r($this->insert_id);
        return $this->insert_id;
    }

How do I pass the variables into the method? I'm trying this ...

$connect = new db();
$db = new nodeModel($connect);
$db->setNodeName('My Node Title');
$db->setNodeLink('My Node Link');
$db->setNodeComment('My Node Comment. This one should be longer so I will write more stuff');
$db->insertNode($db->getNodeName(), $db->getNodeLink(), $db->getNodeComment());

But that's not working. My confusion is really about OO scope inside of class methods ... I'm not sure what I'm I'm supposed to be passing.

6
  • Note: this is another version of the insertNode() that didn't work: $this->insert = $this->mysqli->query("INSERT INTO node(node_name, node_link, node_comment) VALUES ('$this->node_name', '$this->node_link', '$this->node_comment')"); if($this->insert){ echo "Insert Worked.\n"; } Commented Jun 13, 2012 at 23:17
  • In other words, its not working with either a prepared statement or a regular query ... but its not reporting a mysqli_connect_errno() from the constructor Commented Jun 13, 2012 at 23:20
  • Oh, and this is running on a localhost, WAMP to be specific Commented Jun 13, 2012 at 23:21
  • You should be looking for the OOP version of mysqli_connect_errno which is $this->mysqli->connect_errno... also why not just extend db into nodeModel plus there is no connection parrams set Commented Jun 13, 2012 at 23:24
  • You re right, its not recognizing the connect_errno property either ... so I'm NOT making a connection to the db ... let me work on that. Thx Commented Jun 13, 2012 at 23:30

1 Answer 1

1

Try this, with dependency injection pass the connection object to the model:

<?php 
class db{
    protected $mysqli;
    function __construct($host,$username,$password,$database) {
        if(!$this->mysqli instanceof mysqli){
            $this->mysqli = new mysqli($host, $username, $password, $database);
            if ($this->mysqli->connect_errno) {
                die("Failed to connect to MySQL: (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error);
            }
        }
    }
}

class nodeModel{
    function __construct($connection) {
        $this->mysqli = $connection;
    }

    function status() {
        return print_r($this,true);
    }
}
//Create the database object
$connect = new db('localhost','root','password','db');

//Inject the database object into the model
$db = new nodeModel($connect);

//Example method inside the model class
print_r($db->status());
/*
nodeModel Object
(
    [mysqli] => db Object
        (
            [mysqli:db:private] => mysqli Object
                (
                    [affected_rows] => 0
                    [client_info] => mysqlnd 5.0.8-dev - 20102224 - $Revision: 310735 $
                    [client_version] => 50008
                    [connect_errno] => 0
                    [connect_error] => 
                    [errno] => 0
                    [error] => 
                    [field_count] => 0
                    [host_info] => localhost via TCP/IP
                    [info] => 
                    [insert_id] => 0
                    [server_info] => 5.5.16
                    [server_version] => 50516
                    [sqlstate] => 00000
                    [protocol_version] => 10
                    [thread_id] => 28
                    [warning_count] => 0
                )

        )

)
*/
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ... that is working. Though i moved the db values into the db class because that was my intention - not to have to declare them every time I made a call to the db. But I have another question ... please see below ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.