|
Posted by Daz on 02/25/07 15:41
Hi everyone.
I am trying to create an extension of the mysqli class within PHP, and
I am finding it quite difficult. I am fairly new to PHP classes, and
decided to give them a go. Here's what I have to far:
<?php
class sql_db extends mysqli
{
var $connection = false;
function sql_db($username, $password, $database="",
$server="localhost")
{
$this->connection = new mysqli($server, $username,
$password, $database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n",
mysqli_connect_error());
exit();
}
return = $this->connection;
}
}
?>
All I am trying to achieve for now is for a simple error to be printed
if the connection to the database fails. Rather than have error
checking for each within each PHP file, I would just like to have it
all done systematically. I would also like to add a few of my own
methods, such as having the object pass back a PHP array, rather than
a MySQL array. I know I can do this with a separate function, but
really I am doing this for the learning experience more than anything.
At the present time, this works:
$db =& new sql_db("myUsername", "myPass", "someDB");
But as soon as I try to query a table, like so:
$res = $db->query("SELECT * FROM `some_table`;");
I get:
Warning: mysqli::query(): Couldn't fetch sql_db in - on line 21
Evidently I am doing something wrong, and I would really appreciate
any pointers. I suspect that I can't actually do what I want to do,
although I can't see why not. In any case, I am sure it's possible,
but I am going about it completely wrong.
Thanks in advance.
Daz.
[Back to original message]
|