473,748 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($m ydb);
$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 3212
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($m ydb);
$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*******@attgl obal.net
=============== ===

Nov 12 '07 #2
On Nov 12, 11:27 pm, Jerry Stuckle <jstuck...@attg lobal.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($m ydb);
$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...@attgl obal.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
3812
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...
2
4717
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 not. sp_select is a simple one-line script that performs "select * from test". If I replace the call to the stored procedure with a select that does the same thing, both queries work and return results. Is this a PHP or MySQL problem?...
6
2081
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 away from some CGI timeouts I've been experiencing by moving from GCI to ISAPI on PHP. So I removed the mapping for the CGI .exe and added in the mapping for the php5isapi.dll, which in C:\PHP. The php_mysqli.dll file is in C:\PHP\ext. I made...
26
4692
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 Perl like so, and it's pretty to me (and the using of the heredocs I think does defend perl against many arguments withthe HTML being all escaped and explicit returns and stuff -- which I can see... 'print "<p class=\"text\">stuff</p>\n";' is...
12
4758
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);
13
3716
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();
21
7278
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 have to far: <?php class sql_db extends mysqli { var $connection = false;
2
3344
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 want also to use SQLite. Can you give me some advices? 1. PHP4 is still used. I want to be compatible. 2. There are various methods - MySQL, MySQLi, SQLite, PDO... I have
2
3057
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 s\src/../tests_common/tableSnapshots/1_users.sql The command works fine from the mysql command line client, but when I try to run this with the mysqli query function, I get the following
0
8832
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
9562
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
9386
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
8255
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
6799
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
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
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
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.