PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > How to use mysqli object in object, need advise
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to use mysqli object in object, need advise

Réponse
 
LinkBack Outils de la discussion
Vieux 12/11/2007, 22h37   #1
Michael
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
}

}

  Réponse avec citation
Vieux 12/11/2007, 23h27   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to use mysqli object in object, need advise

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

  Réponse avec citation
Vieux 13/11/2007, 16h03   #3
Michael
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to use mysqli object in object, need advise

On Nov 12, 11:27 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> 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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h52.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09948 seconds with 11 queries