473,883 Members | 2,049 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 7300
Daz
On Feb 25, 5:15 pm, "peter" <sub...@flexiwe bhost.comwrote:
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::constru ct(...)

Just a correction of Jerry's post. As of php 5 the __constructor method was
made available BUT the old constructor name is still useable.

The class you are actually using (mysqli) does in fact use a contructor
called mysqli as the following code snippet will demonstrate if you run it:-

<?php
function output_methods( $obj)
{
$methods = get_class_metho ds($obj);
foreach ($methods as $method)
{
echo "function $method()\n";
}

}

output_methods( "mysqli");
?>

I have just one more question. Seeing that you can print methods, how
would I print the code from a function?

Many thanks.

Daz.

Feb 25 '07 #11
peter wrote:
>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::constru ct(...)

Just a correction of Jerry's post. As of php 5 the __constructor method was
made available BUT the old constructor name is still useable.
That's true if there is no __constructor method. However, from what I
understand, the old classname constructor is going away. If you're on
PHP5, you should be using the new way.
The class you are actually using (mysqli) does in fact use a contructor
called mysqli as the following code snippet will demonstrate if you run it:-

<?php
function output_methods( $obj)
{
$methods = get_class_metho ds($obj);
foreach ($methods as $method)
{
echo "function $method()\n";
}
}

output_methods( "mysqli");
?>

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Feb 25 '07 #12
Daz wrote:
On Feb 25, 5:15 pm, "peter" <sub...@flexiwe bhost.comwrote:
>>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::constru ct(...)
Just a correction of Jerry's post. As of php 5 the __constructor method was
made available BUT the old constructor name is still useable.

The class you are actually using (mysqli) does in fact use a contructor
called mysqli as the following code snippet will demonstrate if you run it:-

<?php
function output_methods( $obj)
{
$methods = get_class_metho ds($obj);
foreach ($methods as $method)
{
echo "function $method()\n";
}

}

output_methods ("mysqli");
?>


Excellent! Just what I needed :)

I am wondering why constructors are different in PHP5. Surely anyone
coding portable scripts, would just use the PHP4 constructor
technique, as there's no guarantee that the server it needs to run on
is running PHP5. Some people have no control over what version of PHP
is installed if they are simply paying for a basic hosting account.
Fortunately for me, I have the choice between 4 and 5, but others
won't necessarilly be quite so fortunate.

Thanks for your help Peter. That's sure going to save me digging
through source code and scratching my head to to point of baldness.

Best wishes.

Daz.
Daz,

It was changed because they're moving away from the old classname
constructor to just __constructor. New code on php5 should be using the
new method.

For compatibility with PHP4, just create your constructor as
__constructor. Then create a classname constructor and have it call
__constructor.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Feb 25 '07 #13
..oO(Daz)
>I am wondering why constructors are different in PHP5. Surely anyone
coding portable scripts, would just use the PHP4 constructor
technique
I don't care about PHP 4 anymore. Personally I consider it dead.
>as there's no guarantee that the server it needs to run on
is running PHP5.
My scripts make use of many of the new OOP features in PHP 5, so they
won't run at all on PHP 4.

Micha
Feb 25 '07 #14
I havejustone more question. Seeing that you can print methods, how
would I print the code from a function?
As far as I know you cannot print the code for a class method only
retrieve it's name.

(had to use google groups to post a reply as for some some reason my
news provider is not working properly)

Feb 25 '07 #15
I am wondering why constructors are different in PHP5. Surely anyone
coding portable scripts, wouldjustuse the PHP4 constructor
technique

I don't care aboutPHP4 anymore. Personally I consider it dead.
as there's no guarantee that the server it needs to run on
is running PHP5.

My scripts make use of many of the new OOP features inPHP5, so they
won't run at all onPHP4.

Micha
That is all good and well if you are only coding for yourself but as
the following stats page shows php 4 still has 85% or the market
(although it is decreasing at an ever faster rate)

http://www.nexen.net/chiffres_cles/p...olution.global

Feb 25 '07 #16
..oO(ad***@faho st.com)
>My scripts make use of many of the new OOP features inPHP5, so they
won't run at all onPHP4.

That is all good and well if you are only coding for yourself but as
the following stats page shows php 4 still has 85% or the market
(although it is decreasing at an ever faster rate)
I'm not only coding for myself, but I clearly state that my scripts
require a PHP 5 server and usually I'm the one who decides where to host
a site.

Similarly you can't expect modern software to run on an old system like
Windows 3.1 anymore. And PHP 4 _is_ old. At some point you simply have
to draw the line.

Micha
Feb 26 '07 #17
Michael Fesser wrote:
.oO(Daz)
>I am wondering why constructors are different in PHP5. Surely anyone
coding portable scripts, would just use the PHP4 constructor
technique

I don't care about PHP 4 anymore. Personally I consider it dead.
>as there's no guarantee that the server it needs to run on
is running PHP5.

My scripts make use of many of the new OOP features in PHP 5, so they
won't run at all on PHP 4.

Micha
Micha,

Maybe you consider it dead. But there are probably hundreds of
thousands of shared ISP's out there who disagree with you. And PHP 4 is
not past end-of-life yet, according to PHP.NET.

A good consultant takes all this into consideration, also.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Feb 26 '07 #18
..oO(Jerry Stuckle)
>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.
Maybe you consider it dead. But there are probably hundreds of
thousands of shared ISP's out there who disagree with you.
Sure, but I don't write my scripts for such servers.

I don't know about the situation where you are, but at least here in
Germany offering PHP 5 support is a _must_ for a reputable host.
>And PHP 4 is
not past end-of-life yet, according to PHP.NET.
PHP 6 is going to see the light of day.
>A good consultant takes all this into consideration, also.
A good consultant also knows when it's time for an important upgrade or
a host change. PHP 5 not only has much more features than PHP 4, but is
also faster, more secure and more flexible.

Micha
Feb 26 '07 #19
Michael Fesser wrote:
.oO(Jerry Stuckle)
>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.
Maybe you consider it dead. But there are probably hundreds of
thousands of shared ISP's out there who disagree with you.

Sure, but I don't write my scripts for such servers.

I don't know about the situation where you are, but at least here in
Germany offering PHP 5 support is a _must_ for a reputable host.
>And PHP 4 is
not past end-of-life yet, according to PHP.NET.

PHP 6 is going to see the light of day.
>A good consultant takes all this into consideration, also.

A good consultant also knows when it's time for an important upgrade or
a host change. PHP 5 not only has much more features than PHP 4, but is
also faster, more secure and more flexible.

Micha
Micha,

No arguments about it being faster, etc. However, the market doesn't
always upgrade to the latest. There are a lot of hosts still running
PHP4, all over the world. And to say you won't program for them means
you're missing an awful lot of customers.

But hey - it's your loss, not mine.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Feb 26 '07 #20

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
9797
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11157
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...
0
10763
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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
9586
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...
1
7978
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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...
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

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.