PHP - afficher des zéros si les données sont absentes de la DB

J’aurais besoin d’un peu d’aide de votre part…

Voici un page qui fonctionne, remarquez le fait qu’il y a des données pour la saison 1 et la saion 2.: www.lvhq.com…

Voici une page qui ne fonctionne pas… www.lvhq.com… . Le problème est que le script tente d’aller chercher les stats de la saison 1 mais comme le joueurs n’est pas encore existant pour la saison 1 (donc pas dans la table de la saison 1) il fait cette erreur.

En gros, j’aimerais que PHP print 0 s’il ne peut pas trouver la donnée dans la base, comment faire?

MErci
Edité le 30/10/2007 à 20:54

T’as pas oublié par hasard de mettre le lien vers la page qui ne fonctionne pas?

Au passage, peux tu mettre un titre plus explicite, s’il te plaît?

Merci.

Le lien est corrigé, pour le titre j’aurais bien voulu mais je n’ai pas une idée plus précise…

Je peux t’envoyer le bout de code en message privée au besoin
Edité le 30/10/2007 à 20:54

Poste plutôt ton bout de code dans ce topic, qu’on puisse t’aider :wink:

//Query Overall & Actual STHS Stats //

$QueryActualStats="SELECT Team, Name, Country, Age, Weight, Height, Position, Contract, Rookie, Salary, CK, FG, DI, SK, ST, DU, PH, FO, PA, SC, DF, EX, LD, MO, PO, ProGP, ProShots, ProGoal, ProAssist, ProPlusMoins, ProPim, ProShotsBlock, ProHits, ProHitTook, ProPP, ProSH, ProGW, ProGT, ProFOWon, ProFOTotal, ProPenaltyShotScore, ProPenaltyShotTotal, ProEmptyNetGoal, ProMinutesPlayed, ProHatTrick, FarmGP, FarmShots, FarmGoal, FarmAssist, FarmPlusMoins, FarmPim, FarmShotsBlock, FarmHits, FarmHitTook, FarmPP, FarmSH, FarmGW, FarmGT, FarmFOWon, FarmFOTotal, FarmPenaltyShotsScore, FarmPenaltyShotsTotal, FarmEmptyNetGoal, FarmMinutesPlayed, FarmHatTrick   FROM GDEPlayersStats WHERE Name Like '%$PlayerName%'";


$ResultActualStats=mysql_query($QueryActualStats);
$NumActualStats=mysql_num_rows($ResultActualStats);
$Team = mysql_result($ResultActualStats,$i,"Team");
$PlayerName = mysql_result($ResultActualStats,$i,"Name");
$Country = mysql_result($ResultActualStats,$i,"Country");
$Age = mysql_result($ResultActualStats,$i,"Age");
$Weight = mysql_result($ResultActualStats,$i,"Weight");
$Height = mysql_result($ResultActualStats,$i,"Height");
if ($Height=='64') {$HeightTexte='5' 4&quot';}
if ($Height=='65') {$HeightTexte='5' 5&quot';}
if ($Height=='66') {$HeightTexte='5' 6&quot';}
if ($Height=='67') {$HeightTexte='5' 7&quot';}
if ($Height=='68') {$HeightTexte='5' 8&quot';}
if ($Height=='69') {$HeightTexte='5' 9&quot';}
if ($Height=='70') {$HeightTexte='5' 10&quot';}
if ($Height=='71') {$HeightTexte='5' 11&quot';}
if ($Height=='72') {$HeightTexte='6'';}
if ($Height=='73') {$HeightTexte='6' 1&quot';}
if ($Height=='74') {$HeightTexte='6' 2&quot';}
if ($Height=='75') {$HeightTexte='6' 3&quot';}
if ($Height=='76') {$HeightTexte='6' 4&quot';}
if ($Height=='77') {$HeightTexte='6' 5&quot';}
if ($Height=='78') {$HeightTexte='6' 6&quot';}
if ($Height=='79') {$HeightTexte='6' 7&quot';}
if ($Height=='80') {$HeightTexte='6' 8&quot';}
if ($Height=='81') {$HeightTexte='6' 9&quot';}
if ($Height=='82') {$HeightTexte='6' 10&quot';}
if ($Height=='83') {$HeightTexte='6' 11&quot';}
if ($Height=='84') {$HeightTexte='7'';}
if ($Height=='85') {$HeightTexte='7' 1&quot';}

$Position = mysql_result($ResultActualStats,$i,"Position");
$Rookie = mysql_result($ResultActualStats,$i,"Rookie");
if ($Rookie=='False') {$RookieTexte='Veteran';}
if ($Rookie=='True') {$RookieTexte='Rookie';}

$Contract = mysql_result($ResultActualStats,$i,"Contract");
if ($Contract=='1' AND $Age>='28') {$ContractTexte='<font color="red"><b>UFA</font></b>';}
if ($Contract=='1' AND $Age<'28') {$ContractTexte='<font color="red"><b>RFA 2</font></b>';}
if ($Contract=='1' AND $Age<='23') {$ContractTexte='<font color="red"><b>RFA 1</font></b>';}
if ($Contract!='1') {$ContractTexte=$Contract;}

$Salary = mysql_result($ResultActualStats,$i,"Salary");
$CK = mysql_result($ResultActualStats,$i,"CK");
$FG = mysql_result($ResultActualStats,$i,"FG");
$DI = mysql_result($ResultActualStats,$i,"DI");
$SK = mysql_result($ResultActualStats,$i,"SK");
$ST = mysql_result($ResultActualStats,$i,"ST");
$DU = mysql_result($ResultActualStats,$i,"DU");
$PH = mysql_result($ResultActualStats,$i,"PH");
$FO = mysql_result($ResultActualStats,$i,"FO");
$PA = mysql_result($ResultActualStats,$i,"PA");
$SC = mysql_result($ResultActualStats,$i,"SC");
$DF = mysql_result($ResultActualStats,$i,"DF");
$EX = mysql_result($ResultActualStats,$i,"EX");
$LD = mysql_result($ResultActualStats,$i,"LD");
$MO = mysql_result($ResultActualStats,$i,"MO");
if ($MO<='20') {$MOTexte='Suicidaire';}
if ($MO>'21') {$MOTexte='D&eacute;pressif';}
if ($MO>'35') {$MOTexte='Malheureux';}
if ($MO>'45') {$MOTexte='Normal';}
if ($MO>'55') {$MOTexte='Content';}
if ($MO>'65') {$MOTexte='Heureux';}
if ($MO>'75') {$MOTexte='En feu';}

$PO = mysql_result($ResultActualStats,$i,"PO");
$ProGP = mysql_result($ResultActualStats,$i,"ProGP");
$ProShots = mysql_result($ResultActualStats,$i,"ProShots");
$ProGoal = mysql_result($ResultActualStats,$i,"ProGoal");
$ProAssist = mysql_result($ResultActualStats,$i,"ProAssist");
$ProPlusMoins = mysql_result($ResultActualStats,$i,"ProPlusMoins");
$ProPim = mysql_result($ResultActualStats,$i,"ProPim");
$ProShotsBlock = mysql_result($ResultActualStats,$i,"ProShotsBlock");
$ProHits = mysql_result($ResultActualStats,$i,"ProHits");
$ProHitTook = mysql_result($ResultActualStats,$i,"ProHitTook");
$ProPP = mysql_result($ResultActualStats,$i,"ProPP");
$ProSH = mysql_result($ResultActualStats,$i,"ProSH");
$ProGW = mysql_result($ResultActualStats,$i,"ProGW");
$ProGT = mysql_result($ResultActualStats,$i,"ProGT");
$ProFOWon = mysql_result($ResultActualStats,$i,"ProFOWon");
$ProFOTotal = mysql_result($ResultActualStats,$i,"ProFOTotal");
$ProPenaltyShotScore = mysql_result($ResultActualStats,$i,"ProPenaltyShotScore");
$ProPenaltyShotTotal = mysql_result($ResultActualStats,$i,"ProPenaltyShotTotal");
$ProEmptyNetGoal = mysql_result($ResultActualStats,$i,"ProEmptyNetGoal");
$ProMinutesPlayed = mysql_result($ResultActualStats,$i,"ProMinutesPlayed");
$ProHatTrick = mysql_result($ResultActualStats,$i,"ProHatTrick");
$ProPts = $ProGoal + $ProAssist;
if ($Position=='2') {$PositionTexte='LW';}
if ($Position=='1') {$PositionTexte='C';}
if ($Position=='3') {$PositionTexte='RW';}
if ($Position=='4') {$PositionTexte='D';}

$ProGPFarm = mysql_result($ResultActualStats,$i,"FarmGP");
$ProShotsFarm = mysql_result($ResultActualStats,$i,"FarmShots");
$ProGoalFarm = mysql_result($ResultActualStats,$i,"FarmGoal");
$ProAssistFarm = mysql_result($ResultActualStats,$i,"FarmAssist");
$ProPlusMoinsFarm = mysql_result($ResultActualStats,$i,"FarmPlusMoins");
$ProPimFarm = mysql_result($ResultActualStats,$i,"FarmPim");
$ProShotsBlockFarm = mysql_result($ResultActualStats,$i,"FarmShotsBlock");
$ProHitsFarm = mysql_result($ResultActualStats,$i,"FarmHits");
$ProHitTookFarm = mysql_result($ResultActualStats,$i,"FarmHitTook");
$ProPPFarm = mysql_result($ResultActualStats,$i,"FarmPP");
$ProSHFarm = mysql_result($ResultActualStats,$i,"FarmSH");
$ProGWFarm = mysql_result($ResultActualStats,$i,"FarmGW");
$ProGTFarm = mysql_result($ResultActualStats,$i,"FarmGT");
$ProFOWonFarm = mysql_result($ResultActualStats,$i,"FarmFOWon");
$ProFOTotalFarm = mysql_result($ResultActualStats,$i,"FarmFOTotal");
//$ProPenaltyShotScoreFarm = mysql_result($ResultActualStats,$i,"FarmPenaltyShotScore");
//$ProPenaltyShotTotalFarm = mysql_result($ResultActualStats,$i,"FarmPenaltyShotTotal");
//$ProEmptyNetGoalFarm = mysql_result($ResultActualStats,$i,"FarmEmptyNetGoal");
//$ProMinutesPlayedFarm = mysql_result($ResultActualStats,$i,"FarmMinutesPlayed");
//$ProHatTrickFarm = mysql_result($ResultActualStats,$i,"FarmHatTrick");
$ProPtsFarm = $ProGoalFarm + $ProAssistFarm;
$TeamName = $Team;
include_once "Includes/LogoTeamName.php";

//Saison 1 STHS Stats //

$QueryActualStats="SELECT Team, Name, Country, Age, Weight, Height, Position, Contract, Salary, CK, FG, DI, SK, ST, DU, PH, FO, PA, SC, DF, EX, LD, PO, MO, ProGP, ProShots, ProGoal, ProAssist, ProPlusMoins, ProPim, ProShotsBlock, ProHits, ProHitTook, ProPP, ProSH, ProGW, ProGT, ProFOWon, ProFOTotal, ProPenaltyShotScore, ProPenaltyShotTotal, ProEmptyNetGoal, ProMinutesPlayed, ProHatTrick, FarmGP, FarmShots, FarmGoal, FarmAssist, FarmPlusMoins, FarmPim, FarmShotsBlock, FarmHits, FarmHitTook, FarmPP, FarmSH, FarmGW, FarmGT, FarmFOWon, FarmFOTotal, FarmPenaltyShotsScore, FarmPenaltyShotsTotal, FarmEmptyNetGoal, FarmMinutesPlayed, FarmHatTrick   FROM GDEPlayersStats1 WHERE Name Like '%$PlayerName%'";

$ResultActualStats=mysql_query($QueryActualStats);
$NumActualStats=mysql_num_rows($ResultActualStats);
$Team1 = mysql_result($ResultActualStats,$i,"Team");
$PlayerName = mysql_result($ResultActualStats,$i,"Name");
$Country1 = mysql_result($ResultActualStats,$i,"Country");
$Age1 = mysql_result($ResultActualStats,$i,"Age");
$Weight1 = mysql_result($ResultActualStats,$i,"Weight");
$Height1 = mysql_result($ResultActualStats,$i,"Height");
$Position1 = mysql_result($ResultActualStats,$i,"Position");
$Contract1 = mysql_result($ResultActualStats,$i,"Contract");
$Salary1 = mysql_result($ResultActualStats,$i,"Salary");
$CK1 = mysql_result($ResultActualStats,$i,"CK");
$FG1 = mysql_result($ResultActualStats,$i,"FG");
$DI1 = mysql_result($ResultActualStats,$i,"DI");
$SK1 = mysql_result($ResultActualStats,$i,"SK");
$ST1 = mysql_result($ResultActualStats,$i,"ST");
$DU1 = mysql_result($ResultActualStats,$i,"DU");
$PH1 = mysql_result($ResultActualStats,$i,"PH");
$FO1 = mysql_result($ResultActualStats,$i,"FO");
$PA1 = mysql_result($ResultActualStats,$i,"PA");
$SC1 = mysql_result($ResultActualStats,$i,"SC");
$DF1 = mysql_result($ResultActualStats,$i,"DF");
$EX1 = mysql_result($ResultActualStats,$i,"EX");
$LD1 = mysql_result($ResultActualStats,$i,"LD");
$PO1 = mysql_result($ResultActualStats,$i,"PO");
$MO1 = mysql_result($ResultActualStats,$i,"MO");
$Pro1GP = mysql_result($ResultActualStats,$i,"ProGP");
$Pro1Shots = mysql_result($ResultActualStats,$i,"ProShots");
$Pro1Goal = mysql_result($ResultActualStats,$i,"ProGoal");
$Pro1Assist = mysql_result($ResultActualStats,$i,"ProAssist");
$Pro1PlusMoins = mysql_result($ResultActualStats,$i,"ProPlusMoins");
$Pro1Pim = mysql_result($ResultActualStats,$i,"ProPim");
$Pro1ShotsBlock = mysql_result($ResultActualStats,$i,"ProShotsBlock");
$Pro1Hits = mysql_result($ResultActualStats,$i,"ProHits");
$Pro1HitTook = mysql_result($ResultActualStats,$i,"ProHitTook");
$Pro1PP = mysql_result($ResultActualStats,$i,"ProPP");
$Pro1SH = mysql_result($ResultActualStats,$i,"ProSH");
$Pro1GW = mysql_result($ResultActualStats,$i,"ProGW");
$Pro1GT = mysql_result($ResultActualStats,$i,"ProGT");
$Pro1FOWon = mysql_result($ResultActualStats,$i,"ProFOWon");
$Pro1FOTotal = mysql_result($ResultActualStats,$i,"ProFOTotal");
$Pro1PenaltyShotScore = mysql_result($ResultActualStats,$i,"ProPenaltyShotScore");
$Pro1PenaltyShotTotal = mysql_result($ResultActualStats,$i,"ProPenaltyShotTotal");
$Pro1EmptyNetGoal = mysql_result($ResultActualStats,$i,"ProEmptyNetGoal");
$Pro1MinutesPlayed = mysql_result($ResultActualStats,$i,"ProMinutesPlayed");
$Pro1HatTrick = mysql_result($ResultActualStats,$i,"ProHatTrick");
$Pro1Pts = $Pro1Goal + $Pro1Assist;
if ($Position=='2') {$PositionTexte='LW';}
if ($Position=='1') {$PositionTexte='C';}
if ($Position=='3') {$PositionTexte='RW';}
if ($Position=='4') {$PositionTexte='D';}
$Pro1GPFarm = mysql_result($ResultActualStats,$i,"FarmGP");
$Pro1ShotsFarm = mysql_result($ResultActualStats,$i,"FarmShots");
$Pro1GoalFarm = mysql_result($ResultActualStats,$i,"FarmGoal");
$Pro1AssistFarm = mysql_result($ResultActualStats,$i,"FarmAssist");
$Pro1PlusMoinsFarm = mysql_result($ResultActualStats,$i,"FarmPlusMoins");
$Pro1PimFarm = mysql_result($ResultActualStats,$i,"FarmPim");
$Pro1ShotsBlockFarm = mysql_result($ResultActualStats,$i,"FarmShotsBlock");
$Pro1HitsFarm = mysql_result($ResultActualStats,$i,"FarmHits");
$Pro1HitTookFarm = mysql_result($ResultActualStats,$i,"FarmHitTook");
$Pro1PPFarm = mysql_result($ResultActualStats,$i,"FarmPP");
$Pro1SHFarm = mysql_result($ResultActualStats,$i,"FarmSH");
$Pro1GWFarm = mysql_result($ResultActualStats,$i,"FarmGW");
$Pro1GTFarm = mysql_result($ResultActualStats,$i,"FarmGT");
$Pro1FOWonFarm = mysql_result($ResultActualStats,$i,"FarmFOWon");
$Pro1FOTotalFarm = mysql_result($ResultActualStats,$i,"FarmFOTotal");
//$ProPenaltyShotScoreFarm = mysql_result($ResultActualStats,$i,"FarmPenaltyShotScore");
//$ProPenaltyShotTotalFarm = mysql_result($ResultActualStats,$i,"FarmPenaltyShotTotal");
//$ProEmptyNetGoalFarm = mysql_result($ResultActualStats,$i,"FarmEmptyNetGoal");
//$ProMinutesPlayedFarm = mysql_result($ResultActualStats,$i,"FarmMinutesPlayed");
//$ProHatTrickFarm = mysql_result($ResultActualStats,$i,"FarmHatTrick");
$Pro1PtsFarm = $ProGoalFarm + $ProAssistFarm;
include_once "Includes/LogoTeamName.php";

Le probleme c que dans la DB GDEPlayersStats il y a des joueurs qui ne sont pas présent dans GDEPlayersStats1… Donc lorsqu’il veut setter les variables 1, il ne trouve rien dans la DB

Ouch, y’a du travail de réécriture à faire là

Déjà :

$QueryActualStats="SELECT Team, Name, Country, Age, Weight, Height, Position, Contract, Salary, CK, FG, DI, SK, ST, DU, PH, FO, PA, SC, DF, EX, LD, PO, MO, ProGP, ProShots, ProGoal, ProAssist, ProPlusMoins, ProPim, ProShotsBlock, ProHits, ProHitTook, ProPP, ProSH, ProGW, ProGT, ProFOWon, ProFOTotal, ProPenaltyShotScore, ProPenaltyShotTotal, ProEmptyNetGoal, ProMinutesPlayed, ProHatTrick, FarmGP, FarmShots, FarmGoal, FarmAssist, FarmPlusMoins, FarmPim, FarmShotsBlock, FarmHits, FarmHitTook, FarmPP, FarmSH, FarmGW, FarmGT, FarmFOWon, FarmFOTotal, FarmPenaltyShotsScore, FarmPenaltyShotsTotal, FarmEmptyNetGoal, FarmMinutesPlayed, FarmHatTrick FROM GDEPlayersStats1 WHERE Name Like '%$PlayerName%'";

$ResultActualStats=mysql_query($QueryActualStats);

$NumActualStats = mysql_num_rows($resultActualStats);

if ($NumActualStats == 0)
{
//Aucun joueur trouvé, à toi de voir que faire dans ce cas là
echo 'Aucun joueur trouvé !';
}else
{
 $Pro1 = mysql_fetch_array($ResultActualStats);

//A partir de là, tu accèdes à tes données comme ça, par exemple :
$Pro1PtsFarm = $Pro1['FarmGoal'] + $Pro1['FarmAssist'];
}

Ca devrait déjà pas mal réduire la longueur de ton code, et améliorer la lisibilité et la maintenance :slight_smile:
Edité le 30/10/2007 à 21:36

Merci pour le conseil de structure mais ça ne règle pas du tout mon problème…