Pb Connexion Mysql au sein d'une classe

Petit nouveau ! | 1 Messages

03 nov. 2006, 16:53

Bonjour,

J'ai un fichier index.php principal

dans celui-ci
require("commun/connexions_bd.php"); // connexion Mysql
require("commun/client.php"); // classe client

Fichier connexions_bd.php
global $bd_name, $bd_host, $bd_login, $bd_password, $connect;

$bd_name = "nombd";
$bd_host = "serveur";
$bd_login = "login";
$bd_password = "mdp";

$connect = mysql_connect($bd_host,$bd_login,$bd_password);
if (!$connect)
{
	die ("Cannot connect to MySQL database '$bd_name");
}
if(! mysql_select_db($bd_name,$connect))
{
	echo mysql_error($connect);
}

Fichier client.php
class client
{
   var $ci_id;
   var $ci_nom;
   var $ci_prenom;
   var $ci_naissance;
   var $ci_adresse;
   var $ci_cp;
   var $ci_ville;
   var $ci_email;
   var $ci_tel;
   var $ci_newsletter;
   var $ci_mdp;
   var $ci_date_crea;

   function client($ci_nom,$ci_prenom,$ci_naissance,$ci_adresse,$ci_cp,$ci_ville,$ci_email,$ci_tel,$ci_newsletter,$ci_mdp,$ci_date_crea)
   {
	   $this->ci_id = $ci_id;
	   $this->ci_nom = $ci_nom;
	   $this->ci_prenom = $ci_prenom;
	   $this->ci_naissance = $ci_naissance;
	   $this->ci_adresse = $ci_adresse;
	   $this->ci_cp = $ci_cp;
	   $this->ci_ville = $ci_ville;
	   $this->ci_email = $ci_email;
	   $this->ci_tel = $ci_tel;
	   $this->ci_newsletter = $ci_newsletter;
	   $this->ci_mdp = $ci_mdp;
	   $this->ci_date_crea = $ci_date_crea;
   }

   function affiche()
   {
	   echo("$this->ci_id<br>");
	   echo("$this->ci_nom<br>");
	   echo("$this->ci_prenom<br>");
	   echo("$this->ci_naissance<br>");
	   echo("$this->ci_adresse<br>");
	   echo("$this->ci_cp<br>");
	   echo("$this->ci_ville<br>");
	   echo("$this->ci_email<br>");
	   echo("$this->ci_tel<br>");
	   echo("$this->ci_newsletter<br>");
	   echo("$this->ci_mdp<br>");
	   echo("$this->ci_date_crea<br>");
   }

   function creation_en_base()
   {
	$ci_date_crea = date("Y-m-d");
	$req="insert into client_indiv (CI_NOM,CI_PRENOM,CI_NAISSANCE,CI_ADRESSE,CI_CP,CI_VILLE,CI_EMAIL,CI_TEL,CI_NEWSLETTER,CI_MDP,CI_DATE_CREA) VALUES ('$this->ci_nom','$this->ci_prenom','$this->ci_naissance','$this->ci_adresse',$this->ci_cp,'$this->ci_ville','$this->ci_email',$this->ci_tel,$this->ci_newsletter,'$this->ci_mdp','$ci_date_crea')";
	$rescode = mysql_query($req,$connect);
	echo mysql_error($connect);
   }
}

La méthode création_en_base ne fonctionne pas. Je suis obligé de redéfinir la connexion à la base et lors de l'appel à la méthode, transmettre les paramètres de connexion.
....

   function creation_en_base($bd_name, $bd_host, $bd_login, $bd_password)
   {
	$connect = mysql_connect($bd_host,$bd_login,$bd_password);
	if (!$connect)
	{
		die ("Cannot connect to MySQL database '$bd_name");
	}
	if(! mysql_select_db($bd_name,$connect))
	{
		echo mysql_error($connect);
	}
   	$ci_date_crea = date("Y-m-d");
	$req="insert into client_indiv (CI_NOM,CI_PRENOM,CI_NAISSANCE,CI_ADRESSE,CI_CP,CI_VILLE,CI_EMAIL,CI_TEL,CI_NEWSLETTER,CI_MDP,CI_DATE_CREA) VALUES ('$this->ci_nom','$this->ci_prenom','$this->ci_naissance','$this->ci_adresse',$this->ci_cp,'$this->ci_ville','$this->ci_email',$this->ci_tel,$this->ci_newsletter,'$this->ci_mdp','$ci_date_crea')";
	$rescode = mysql_query($req,$connect);
	echo mysql_error($connect);
   }
}
Est-ce normal ? ou y a-t-il une solution ?

Merci par avance pour votre aide!

Eléphanteau du PHP | 21 Messages

03 nov. 2006, 17:06

si tu ne precise pas $connect à l'appel du mysql_query() php va te selectionner la derniere connexion établie si elle n'est pas fermée.. enfin il me semble..
sinon dans ta méthode tu peux définir un global $connect pour recupérer ta variable globale

Invité
Invité n'ayant pas de compte PHPfrance

03 nov. 2006, 18:56

En redéfinissant mes variables en $global effectivement ca fonctionne.

Merci!