|
Posted by Jerry Stuckle on 02/25/07 16:10
Daz wrote:
> On Feb 25, 3:55 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> Daz wrote:
>>> 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.
>> Daz,
>>
>> If you're going to extend a class, you don't use:
>>
>> $this->connection = new mysqli($server, $username,
>> $password, $database);
>>
>> You already have an instance of mysqli via inheritance; this would be
>> creating another one.
>>
>> Rather, you should be using
>>
>> parent::mysqli($server, $username, $password, $database);
>>
>> (Assuming PHP4 from your previous post).
>>
>> You also don't need to keep the $connection - mysqli will maintain that
>> info..
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
>
> Thanks for that Jerry. That gives me something to go on. I am using
> PHP5, as I believe PHP4 doesn't support the mysqli object (but I am
> most likely wrong).
>
> Thanks again for your assistance.
>
> Daz.
>
Daz,
Actually, PHP4 does support the mysqli object, also.
But in PHP5 your constructors are named __construct, not the class name.
So you need to change the constructor name in your class, and call
parent::construct(...)
instead.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|