I am 开发者_开发问答trying to use mysqli in object orientated way, this is the code:
class conect {
function populate( ) {
while ($row = $this->result->fetch_object()) { $this->data[] = $row; }
$this->result->close();
}
function conect( ) {
$mysqli = new mysqli('localhost', 'root', 'root', 'todo');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
};
}
function register($name, $email, $password ) {
$mysqli->query("INSERT INTO user (name, email, password)
VALUES ('$name', '$email', '$password')");
}
}
The register function is the problem, if I add that query in the conect() function it does work, but as it id does not work,no error is displayed
I have been trying to learn object orientated php and mysqli. I have managed to use the above style to create a dynamic portfolio, but this is the first time I tried to write to DB so I dont know whats the problem
Hope you can help
Thanks
you don't display any errors, so, no wonder there are no errors displayed.
$sql = "INSERT INTO user (name, email, password) VALUES ('$name', '$email', '$password')";
if (!$mysqli->query($sql)) {
trigger_error($mysqli->error);
}
but, to let you know, your code has nothing to do with mysqli and oop anyway.
The problem is that your variable $mysqli
inside your register()
method isn't defined. This also means that you cannot call any method from "nothing". Add error_reporting(E_ALL);
at the top of your script to see warnings about undefined variables.
To solve this problem you want to save the MySQLi object in an object field and access it with $this->mysqli
or sth. That way you can set it in your connect()
method and use it in your register()
method.
精彩评论