|
Posted by Jerry Stuckle on 02/25/07 16:17
Daz wrote:
> On Feb 25, 3:41 pm, "Daz" <cutenfu...@gmail.com> 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.
>
>
> OK, I've done some more thinking, and I think that what I am trying to
> do, would involve overriding the constructor of the mysqli class,
> which I guess wouldn't be wise, and might not even be possible.
>
> Is there any way to override the contructor and find out what the
> constructor code is? I guess that it wouldn't be a PHP class as such,
> but more of a hybrid, but I am uncertain.
>
> I can see that what I am doing is correct for adding extra functions,
> but I would appreciate it if anyone can help me figure out the best
> way to do some error checking when the connection is opened.
>
> I would also appreciate any input on overriding the default methods,
> i.e whether it's possible or not, or whether it's ill advised, or if I
> can actually use the original constructor and carefully modify it.
>
> I think the initial error checking might require an external function
> which instantiates the class, and does the checking, then returns the
> mysqli object if all went well, or returns false if it didn't.
>
Daz,
Overriding the constructor is quite common. But you need to ensure you
also call the parent class constructor.
As to seeing the code - it's like anything else in PHP. If it's a PHP
file, you can see the code. If, like with mysqli, it's a compiled
extension, you need to look at the source code for the extension.
If you want some error checking when you open the connection, see if the
connection is open after calling the mysqli constructor (see if
mysqli_connect_error() is non-zero).
You don't need an external function - you just need to check for errors.
And if you don't want to keep checking the responses, check you
exception handling.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|