473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to use mysqli object in object, need advise

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
<?

$a = new MyClass();

//here other stuf with connections to other databases

$a->connectToDB($mydb);
$a->methode1();

//and so on...

?>
class MyClass{
var one = '';
var mysqli = '';

function __construct(){
$this->mysqli = new mysqli() //with all needed parameters
}
function connectToDB($db){
$this->mysqli->connect() //with all needed parameters

}

function methode1(){
//my stuff here with mysqli query
}

function methode2(){
/ /my stuff here with another mysqli query
}

}

Nov 12 '07 #1
2 3179
Michael wrote:
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
<?

$a = new MyClass();

//here other stuf with connections to other databases

$a->connectToDB($mydb);
$a->methode1();

//and so on...

?>
class MyClass{
var one = '';
var mysqli = '';

function __construct(){
$this->mysqli = new mysqli() //with all needed parameters
}
function connectToDB($db){
$this->mysqli->connect() //with all needed parameters

}

function methode1(){
//my stuff here with mysqli query
}

function methode2(){
/ /my stuff here with another mysqli query
}

}

Michael,

First of all, you'll find it easier to derive your class from mysqli.
You'll then be able to call the mysqli functions directly from your
program (or override them in your class, as necessary).

For instance, you could create a connect() function with calls
parent::connect() with the appropriate parameters.

But either way, you should only need to connect when you create a new
MyClass object. If you use the same object multiple places in your
page, you should be able to just keep using the same connection.

Unfortunately, you didn't show us more of the code so we can see what's
going wrong.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 12 '07 #2
On Nov 12, 11:27 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Michael wrote:
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
<?
$a = new MyClass();
//here other stuf with connections to other databases
$a->connectToDB($mydb);
$a->methode1();
//and so on...
?>
class MyClass{
var one = '';
var mysqli = '';
function __construct(){
$this->mysqli = new mysqli() //with all needed parameters
}
function connectToDB($db){
$this->mysqli->connect() //with all needed parameters
}
function methode1(){
//my stuff here with mysqli query
}
function methode2(){
/ /my stuff here with another mysqli query
}
}

Michael,

First of all, you'll find it easier to derive your class from mysqli.
You'll then be able to call the mysqli functions directly from your
program (or override them in your class, as necessary).

For instance, you could create a connect() function with calls
parent::connect() with the appropriate parameters.

But either way, you should only need to connect when you create a new
MyClass object. If you use the same object multiple places in your
page, you should be able to just keep using the same connection.

Unfortunately, you didn't show us more of the code so we can see what's
going wrong.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Hi Jerry,

thanks for your suggestions. I derive now my class from mysqli class
and it is indeed much better. My problem was, that I needed connection
to DB in __constructor and forget to initiate it. Anyway, it works as
expected now.

Best regards

Nov 13 '07 #3

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

Similar topics

1
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...
2
by: Greg Schumacher | last post by:
Running PHP v5.0.4 (compiled by www.entropy.ch) and mysql 5.0.16 on Mac OS X 10.4.3. When I run the simple test script below the first query sent from PHP to MySQL is successful but the second is...
6
by: Christopher Watson | last post by:
Have been running PHP 5.1.1 successfully for quite some time as CGI in IIS 5.1 on my Windows XP Pro SP2 laptop, with MySQL Server 5.0.18 and the mysqli extension. I wanted to see if I could get...
26
by: Dodger | last post by:
Okay, background... yes, I am another of those evil, spurned, damnable Perl mongers, but I'm not trying to start a flamewar, I'm juust tryung to understand something... I can write a script in...
12
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');...
13
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...
21
by: Daz | last post by:
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...
2
by: webcm123 | last post by:
People say that structural programming isn't good for database connection. I code fast-running structural oriented CMS and I don't know what I should do. I use mysql connection using mysql_*. I...
2
by: Taras_96 | last post by:
Hi everyone, I'm trying to run a number of commands stored within a sql file from within php using mysqli::query. The syntax I'm using is: source C:\data\projects\forum...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...

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.