echo sur deux tables avec mysql_fetch_array

patchap40
Invité n'ayant pas de compte PHPfrance

08 mars 2006, 16:33

Bonjour à tous, j'aimerai afficher des informations provenant de deux tables différentes avec un mysql_fetch_array, mais j'y arrive pas car je parcours une seule table à la fois

Voici ma premiere table stage

Code : Tout sélectionner

CREATE TABLE `Stage` ( `id_sta` int(3) NOT NULL default '0', `statut_sta` int(1) NOT NULL default '0', `lieu_sta` varchar(70) NOT NULL default '', `theme_sta` varchar(100) NOT NULL default '', `dateDeb_sta` date NOT NULL default '0000-00-00', `dateFin_sta` date NOT NULL default '0000-00-00', `nbInfo_sta` int(2) NOT NULL default '0', `descr_sta` text NOT NULL, `config_sta` text NOT NULL, PRIMARY KEY (`id_sta`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
La deuxième

Code : Tout sélectionner

CREATE TABLE `Entreprise` ( `id_ent` int(3) NOT NULL default '0', `statut_ent` int(1) NOT NULL default '0', `pour_ent` int(1) NOT NULL default '0', `nom_ent` varchar(50) NOT NULL default '', `adresse_ent` varchar(70) NOT NULL default '', `cp_ent` int(5) NOT NULL default '0', `ville_ent` varchar(25) NOT NULL default '', `tel_ent` int(10) unsigned zerofill NOT NULL default '0000000000', `fax_ent` int(10) unsigned zerofill NOT NULL default '0000000000', `mail_ent` varchar(40) NOT NULL default '', `activite_ent` varchar(70) NOT NULL default '', PRIMARY KEY (`id_ent`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Voici mon code

Code : Tout sélectionner

$requete1="SELECT id_sta, statut_sta, theme_sta FROM Stage WHERE statut_sta=1 ORDER BY id_sta ASC"; $requete2="SELECT id_ent, statut_ent, pour_ent, nom_ent FROM Entreprise WHERE statut_ent=1 and pour_ent=0 ORDER BY id_ent ASC"; //Execution de la requete $result1=mysql_query($requete1); $result2=mysql_query($requete2); while($array=mysql_fetch_array($result1)){ $theme=$array['theme_sta']; $nomEnt=$array['nom_ent']; echo "Thème stage : ".$theme."<BR>"; echo "Entreprise : ".$nomEnt."<BR>"; }
Et à l'écran ça me donne

Code : Tout sélectionner

Thème stage : info Entreprise :
Alors que j'aurais aimer que le nom de l'Entreprise s'affiche.
Merci pour vos réponses

Mammouth du PHP | 2937 Messages

08 mars 2006, 17:13

Salut!

Essaie de réunir tes deux requêtes en une seule, en codant ainsi:
"SELECT * FROM Stage, Entreprise WHERE...";
Et en veillant à préciser la table d'appartenance si deux champs ont le même nom (
Stage.champ
,
Entreprise.champ
).

patchap40
Invité n'ayant pas de compte PHPfrance

08 mars 2006, 17:21

merci ca marche mais maintenant mes infos s'écrivent en double

Code : Tout sélectionner

Thème stage : réseau Entreprise : pino Thème stage : réseau Entreprise : infopratique Thème stage : info Entreprise : pino Thème stage : info Entreprise : infopratique

patchap40
Invité n'ayant pas de compte PHPfrance

08 mars 2006, 17:26

mon code
//Recuperation des donnees pour le stage
$requete1="SELECT * FROM Stage, Entreprise WHERE statut_sta=1 and statut_ent=1 and pour_ent=0 ORDER BY id_sta ASC";

//Execution de la requete
$result=mysql_query($requete1);

while($array=mysql_fetch_array($result)){
	$theme=$array['theme_sta'];
	$nomEnt=$array['nom_ent'];
echo "Thème stage  :   ".$theme."<BR>";
echo "Entreprise   :   ".$nomEnt."<BR>";
	
}

Mammouth du PHP | 2937 Messages

08 mars 2006, 17:30

Re!

Essaie de revoir la mise en page de la page: essaie de faire un tableau dans lequel la boucle while englobe les balises <tr>. En gros:
<table>
<?php
while ($array=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $array["theme_sta"] ?></td>
<td><?php echo $array["nom_ent"] ?></td>
</tr>
<?php
}
?>
</table>

patchap40
Invité n'ayant pas de compte PHPfrance

08 mars 2006, 17:44

voila ce que ça m'affiche maintenant

Code : Tout sélectionner

info pino réseau pino info infopratique réseau infopratique
ça me les alignes, c'est tout. En fait c'est pas des doublons, c'est que pour un thème de stage, deux noms d'entreprise lui sont attribués et inversement (une entr deux thème)

patchap40
Invité n'ayant pas de compte PHPfrance

08 mars 2006, 17:47

c'est bon j'ai réussi ça venait de ma requête
voici mon code
//Recuperation des donnees pour le stage
$requete="SELECT * FROM Stage, Entreprise WHERE statut_sta=1 and statut_ent=1 and pour_ent=0 and id_sta=id_ent";

//Execution de la requete
$result=mysql_query($requete);

while($array=mysql_fetch_array($result)){
	$theme=$array['theme_sta'];
	$nomEnt=$array['nom_ent'];
echo "Thème stage  :   ".$theme."<BR>";
echo "Entreprise   :   ".$nomEnt."<BR>";
	
}
et à l'écran

Code : Tout sélectionner

Thème stage : info Entreprise : pino Thème stage : réseau Entreprise : infopratique
Merci à tous :wink: