I've got a couple of model classes handling a db layer, and I've got a logic error in how to hand the db connection to the query in an OO way. Class1 is a db_connect which I'm calling inside the constructor of class2 query methods:
class db {
public $host = 'localhost';
public $username = 'root';
public $password = '';
public $database = 'molecule';
public $mysqli = '';
function __construct() {
$mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
if (mysqli_connect_errno()) {
printf("Database Connection failed: %s
", mysqli_connect_error());
exit();
}
return $mysqli;
}
}
class nodeModel {
public $node;
public $node_name;
public $node_link;
public $node_comment;
public $mysqli;
function __construct() {
$mysqli = new db;
return $mysqli;
}
public $insert;
public $insert_id;
function insertNode($node_name, $node_link, $node_comment) {
$this->insert = $insert;
$this->insert = $mysqli->prepare("INSERT INTO node(node_name, name_link, node_comment) VALUES (?, ?, ?)");
$this->insert->bind_param("sss", $this->node_name, $this->node_link, $this->node_comment);
if($this->insert->execute()) {
$this->insert_id = $mysqli->insert_id;
}
return $this->insert_id;
print_r($this->insert_id);
$this->insert->close();
}}
But I'm getting an undefined variable error with $insert.
I am calling that layer with the following:
include 'node_model.php';
$dbTest = new nodeModel;
$node_name = 'My Node Title';
$node_link = 'My Node Link';
$node_comment = 'My Node Comment. This one should be longer so I will write more stuff';
$dbTest->insertNode($node_name, $node_link, $node_comment);
The way I'm connecting with the constructor of the query class seems to be working but not sure which variable I need in scope to attach to the prepare/bind/execute statements?
And I do know that I should be using PDO or another ORMy type tool. Which I will, but this is more about some basic OO get/set/retrieve best practice ... thanks for your help.
There are some major problems with Your code...
1. Make Your model class properties private and create setter and getter methods to set or retrieve a value to/from property. Also Your method insertNode is wrong.
class nodeModel {
private $node;
private $nodeName;
private $nodeLink;
private $nodeComment;
private $mysqli;
function __construct() {
$this->mysqli = new db;
}
public function getNodeName() {
return $this->nodeName;
}
public function getNodeLink() {
return $this->nodeLink;
}
public function getNodeComment() {
return $this->nodeComment;
}
public function setNodeName($value) {
$this->nodeName = $value;
}
public function setNodeLink($value) {
$this->nodeLink = $value;
}
public function setNodeComment($value) {
$this->nodeComment = $value;
}
function insertNode() {
$this->insert = $this->mysqli->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;
}}
}
2. In class db
when You create a connection store it into $this->mysqli
, not just $mysqli
.
function __construct() {
$this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
if (mysqli_connect_errno()) {
printf("Database Connection failed: %s
", mysqli_connect_error());
exit();
}
return $this->mysqli;
}
3. In Your third code, you create only a variables that You pass to the method insertNode
but within this method You do nothing with the passed variables - You expect that the class properties are set but they are not... Therefore do this:
include 'node_model.php';
$dbTest = new nodeModel;
$dbTest->setNodeName('My Node Title');
$dbTest->setNodeLink('My Node Link');
$dbTest->setNodeComment('My Node Comment. This one should be longer so I will write more stuff');
$dbTest->insertNode();
Hope this helps...
But I'm getting an undefined variable error with $insert.
That's because the first lines of your function are
$this->insert = $insert;
$this->insert = $mysqli->prepare("INSERT INTO node(node_name, name_link, node_comment) VALUES (?, ?, ?)");
$insert is not in your arguments list, so the first line causes an error. And then in your second line, you overwrite the variable anyway.
You also have 2 lines of code after the return statement, which of course never execute.
Perhaps it's time to get some sleep? :)