You are here: Re: Extending the mysqli class « PHP Programming Language « IT news, forums, messages
Re: Extending the mysqli class

Posted by Jerry Stuckle on 02/25/07 15:55

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.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация