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.
mysqli_connect_errnowhich is$this->mysqli->connect_errno... also why not just extend db into nodeModel plus there is no connection parrams set