473,883 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extending the mysqli class

Daz
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($usernam e, $password, $database="",
$server="localh ost")
{
$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("myUsern ame", "myPass", "someDB");

But as soon as I try to query a table, like so:

$res = $db->query("SELEC T * 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.

Feb 25 '07
21 7301
Daz wrote:
I am wondering why constructors are different in PHP5.
Under PHP 4:

<?php
class A
{
function A ()
{
print "Constructo r called\n";
}
}

class B extends A
{
// The constructor for this class is now a
// function called 'A'. If you were reading the
// code, this might be somewhat confusing, but
// you could logically infer that it's because
// it extends A.
}

class C extends B
{
// The constructor for this class is now a
// function called 'A'. There is no clue within
// this class definition as to why that is! If
// classes A and B were defined in a separate
// file, then there would be no clues in this
// file that the constructor function is called
// 'A'.
}

class D extends C
{
function D ()
{
print "This is D!\n";
parent::C(); // wrong!
}
}
?>

PHP 5 allows all classes to have the same name for their constructor. So
when subclassing an existing class, you can reliably call the parent's
constructor function as "parent::__cont ruct()" without having to look into
the internals of the class that you're extending. (Which is how it should
be with OO programming: classes should be black boxes.)

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 27 '07 #21
Michael Fesser wrote:
My scripts make use of many of the new OOP features in PHP 5, so they
won't run at all on PHP 4.
Ditto.

My current big pet project is http://demiblog.org/. This is PHP 5+ only
and supports MySQL 5+ and PostgreSQL 8+. Although I do realise that earlier
versions of these packages are still quite widely used out there, this
project probably won't hit the big 1.0 for another year or two, by which
time, the hosting world will have probably moved on a lot. Supporting
older versions of PHP and the database engines will cost development time
and push back the project release date even later, by which time PHP 4
support will be even less relevant.

So although PHP 4 may still be alive right now, it's counting down the
days to his retirement when it can hand over the family business to his
son PHP 5. He's heard the happy news that his dranddaughter PHP 6 is
planning on following in their footsteps too as soon as she graduates.
Because of PHP 4's impending retirements, he's not taking on any more big
projects at work, and is letting his son take on most of the new clients.

Analogy over, it would be unwise at this point to look at supporting PHP 4
for any medium-to-large projects that are starting now or in the future.
Existing PHP 4 projects should probably keep supporting PHP 4 for a while
yet, but should certainly make an effort to support PHP 5 as well, and
should be keeping a close eye on PHP 6 developments.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 27 '07 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2062
by: Hayden Kirk | last post by:
Can anyone tell me out to make a new mysqli class. Im going $var = new mysql(); I get an error unknown class, php 5.0.3 Thanks
1
3815
by: Tom D | last post by:
I'm rewriting a database interface that our company currently has. Currently it's using the Pear::DB interface, but we found that that was introducing a bit too much overhead. I'm rewriting the interface to use mysqli. Most of what the interface does is to simplify getting results in the form of arrays (ordered and associative). Most of the code using the interface used sql queries with placeholders and parameters. For that reason I'd...
12
4785
by: davids58 | last post by:
trying to figure out how to use a mysql database with PHP. I ran the following code: <?php // defines database connection data define('DB_HOST', 'localhost'); define('DB_USER', 'ajaxuser'); define('DB_PASSWORD', 'practical'); define('DB_DATABASE', 'ajax'); // connect to the database $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
2
2776
by: Curtis | last post by:
Hello everyone: Recently, I decided to upgrade to PHP 5.2.0. I have C:\php5 in the Windows XP PATH, so upgrading is quite painless; just unzip new release, and restart Apache! Usually it goes off without a hitch, but I noticed that phpMyAdmin was not able to utilize the MySQLi library (which was working with my last 5.1.x release). As I was looking through php_info(), I noticed that MySQLi didn't even load. However, when I restarted...
13
3725
by: Schmidty | last post by:
If you do a page reload with $_SERVER will your program lose a mysqli connection upon the reload of the page? Would this code work? I need to know how to carry over a connection between methods as I am new to OOP? Thanks... Example; ======================================== <?php // webpage $newsignon = new newuser(); logon();
2
2609
by: Curtis | last post by:
Hello everyone: I have come to love the ease of updating PHP, since getting used to using it these past few years. Recently, however, when I upgraded from PHP 5.1 to PHP 5.2.0 and again when moving to 5.2.1, I noticed strange behavior when trying to load php_mysqli.dll. I am using Apache 2.2.3 on Windows XP Pro (32-bit). I use the php5apache2_2.dll to load PHP as an Apache module. The PHPIniDir directive in httpd.conf points to the...
11
11109
by: macca | last post by:
Hi, What should I be using for general MySQL database access? I've been using the traditional mysql extension for ages, but I'm trying to update my style to a more OOP paradigm. I've used PDO briefly but I've not used the mysqli extension yet. I've read a bit about it though, seems good and more OOP orientated (for the most part). But PDO seems more generic and transferable.
2
3216
by: Michael | last post by:
Hi, I try to use mysqli object instead of standard mysql functions. Is it ok to create mysqli object within my class or schould I pass mysqli object to my object. The problem is, with code below I must call mysqli->connect() each time I call class methods. How do I create an connection for hole object, so methods can do queries without connect each time? Best Regards, Michael
3
2318
code green
by: code green | last post by:
I am still using the mysql extension rather than the mysqli. Moving to mysqli would be realively simple in my case because all mysql_ procedural calls are wrapped in a class MySqlDB(). As are mssql_ procedural calls and both inherit from an abstract class that handles common functionallity baseDB(). So to switch I need to edit a few lines in the MySqlDB class. At least I think it is that simple. But as I already have a class inheriting...
0
9945
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10863
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9588
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6007
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.