Using If Statements to Pull different Queries?

Hi There,

I have a php query that pulls data from one type of hockey player and I want it to recognize different positions to pull a different query set.

Right now it looks like this

[php] $iPlayerID = $_GET[“PlayerID”];
$iNationID = $_GET[“NationID”];

$oPlayerInfo = mysql_query("
SELECT Players.PlayerID, Players.FirstName, Players.LastName, Players.Position, Players.Height, Players.Weight, Players.DOB, CNGHLTeams.CNGHLRights, NHLTeams.Team, Players.CNDraftYR, DraftTeam.DraftID, Players.CNDraftPOS, Countries.Nation, Players.NationID, DraftTeam.DrTeam, Players.FullName
FROM Players
Left JOIN CNGHLTeams
ON Players.CNGHLID=CNGHLTeams.CNGHLID
Left JOIN NHLTeams
ON Players.TeamsID=NHLTeams.TeamID
Left JOIN Countries
ON Players.NationID=Countries.NationID
Left JOIN DraftTeam
ON Players.DraftID=DraftTeam.DraftID
WHERE Players.PlayerID=$iPlayerID
ORDER BY Players.LastName;
") or die(mysql_error());
$row = mysql_fetch_array($oPlayerInfo);
?>

<?PHP echo "CNGHLDB - " . $row["FullName"] ?>

img.top {vertical-align:text-top;}
img.bottom {vertical-align:text-bottom;}
a {text-decoration:none;}

<?PHP { Print ""; ECHO "
"; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO "

 

"; Print "

"; Print ""; Print ""; Print ""; Print ""; Print ""; $DOB = $row[DOB]; //dd.mm.yyyy $user_date = new DateTime($DOB); $curr_date = new DateTime(); $age_cal = $curr_date->diff($user_date); Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print " "; Print "

".$row['FirstName']." ".$row['LastName']." 

Birthdate: ".$row['DOB']." Age: ".$age_cal->y;"
"; Print "
Nation: ".$row['Nation']." CNGHL Team: ".$row['CNGHLRights']. "
Position: ".$row['Position']. " Weight: ".$row['Weight']. "
Height: ".$row['Height']. " NHL Team: ".$row['Team']. "
Draft Year: ".$row['CNDraftYR']." Draft Position: ".$row['CNDraftPOS']."
Drafted By: ".$row['DrTeam']." 
 
"; Print "
";[/php] And it Pulls [php]$oPlayerStats = mysql_query(" SELECT SeasonStats.Sea, SeasonStats.TeamID, SeasonStats.GP, SeasonStats.Goals, SeasonStats.Assists, SeasonStats.Points, SeasonStats.PlusMinus, SeasonStats.Pim, SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S, Seasonteam.Team, SeasonStats.SubSeason FROM SeasonStats Left JOIN Seasonteam On SeasonStats.TeamID=Seasonteam.TeamID WHERE SeasonStats.PlayerID=$iPlayerID ORDER BY SeasonStats.Sea ASC,SeasonStats.SubSeason ASC; ") or die(mysql_error()); Print ''; Print "

"; ECHO "
"; ECHO "
"; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; while($row = mysql_fetch_array($oPlayerStats)) { Print ""; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; } Print "
Season
Team
GP
G
A
P
PIM
+/-
PPG
SHG
GWG
GTG
Shots
".$row['Sea']."".$row['Team']."".$row['GP']."".$row['Goals']."".$row['Assists']."".$row['Points']."".$row['Pim']."".$row['PlusMinus']."".$row['PP']."".$row['SH']."".$row['GW']."".$row['GT']."".$row['S']."
"; [/php] But I want it to recognize when the position in the first query is "Goaltender" to pull this query instead of the regular one that I just posted. [php]$oGoalieNewStats = mysql_query(" SELECT GoalieNewStats.Sea, GoalieNewStats.TeamID, GoalieNewStats.GP, GoalieNewStats.Min,GoalieNewStats.GAA, GoalieNewStats.W, GoalieNewStats.L, GoalieNewStats.OTL, GoalieNewStats.T, GoalieNewStats.SO, GoalieNewStats.GA, GoalieNewStats.SA, GoalieNewStats.PCT, GoalieNewStats.PIM, GoalieNewStats.Assists, Seasonteam.Team FROM GoalieNewStats Left JOIN Seasonteam On GoalieNewStats.TeamID=Seasonteam.TeamID WHERE GoalieNewStats.GoalieID=$iGoalieID ORDER BY GoalieNewStats.Sea, GoalieNewStats.SubSeason ASC; ") or die(mysql_error()); Print ''; Print "

"; ECHO "
"; ECHO "
"; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; while($row = mysql_fetch_array($oGoalieNewStats)) { Print ""; Print " "; Print " "; Print " "; Print " "; $iGAA = $row['GA'] * 60 / $row['Min']; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; } Print "
Season
Team"; ECHO "
GP"; ECHO "
Min"; ECHO "
GAA"; ECHO "
W"; ECHO "
L"; ECHO "
OTL"; ECHO "
T"; ECHO "
SO"; ECHO "
GA"; ECHO "
SA"; ECHO "
PCT"; ECHO "
PIM"; ECHO "
Assists"; ECHO "
".$row['Sea']."".$row['Team']."".$row['GP']."".$row['Min']."".$english_format_number = number_format($iGAA, 2)."".$row['W']."".$row['L']."".$row['OTL']."".$row['T']."".$row['SO']."".$row['GA']."".$row['SA']."".$row['PCT']."".$row['PIM']."".$row['Assists']."
"; [/php] Is there a simple way of doing this that I am just not seeing, I am not familiar with IF statement other then in Excel, my thoughts are it should be something like this [php]IF Position = "Goaltender" Select $oGoalieNewStats = mysql_query if not select $oPlayerStats = mysql_query[/php] Thanks for reading my ramblings

If will look something similar to this, this should get you started.

[php]if ($row[‘Position’] == “Goaltender”)
{
$row = mysql_fetch_array($oGoalieNewStats);
}elseif {
$row = mysql_fetch_array( $oPlayerStats);
};[/php]

You would be getting into somewhat advanced level programming, but you can do what you are wanting in the database itself, MySQL IF statements.

The other way, probably more what you are looking for, is to dynamically build the query.

This smells of a bad database design. Post an SQL dump of your db and I will take a look at it.

I tried to use this snippet in my php file and now I just get a blank page, which my guess means that there is nothing to display.

[php]
//Get PlayerID from URL
$iPlayerID = $_GET[“PlayerID”];
$iNationID = $_GET[“NationID”];
$iPositionID= $_GET[“PositionID”];

$oPlayerInfo = mysql_query("
SELECT Players.PlayerID, Players.FirstName, Players.LastName, Players.Position, Players.Height, Players.Weight, Players.DOB, CNGHLTeams.CNGHLRights, NHLTeams.Team, Players.CNDraftYR, DraftTeam.DraftID, Players.CNDraftPOS, Countries.Nation, Players.NationID, DraftTeam.DrTeam, Players.FullName
FROM Players
Left JOIN CNGHLTeams
ON Players.CNGHLID=CNGHLTeams.CNGHLID
Left JOIN NHLTeams
ON Players.TeamsID=NHLTeams.TeamID
Left JOIN Countries
ON Players.NationID=Countries.NationID
Left JOIN DraftTeam
ON Players.DraftID=DraftTeam.DraftID
WHERE Players.PlayerID=$iPlayerID
ORDER BY Players.LastName;
") or die(mysql_error());
$row = mysql_fetch_array($oPlayerInfo);
?>

<?PHP echo "CNGHLDB - " . $row["FullName"] ?>

img.top {vertical-align:text-top;}
img.bottom {vertical-align:text-bottom;}
a {text-decoration:none;}

<?PHP { Print ""; ECHO "
"; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO "

 

"; Print "

"; Print ""; Print ""; Print ""; Print ""; Print ""; $DOB = $row[DOB]; //dd.mm.yyyy $user_date = new DateTime($DOB); $curr_date = new DateTime(); $age_cal = $curr_date->diff($user_date); Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print ""; Print " "; Print "

".$row['FirstName']." ".$row['LastName']." 

Birthdate: ".$row['DOB']." Age: ".$age_cal->y;"
"; Print "
Nation: ".$row['Nation']." CNGHL Team: ".$row['CNGHLRights']. "
Position: ".$row['Position']. " Weight: ".$row['Weight']. "
Height: ".$row['Height']. " NHL Team: ".$row['Team']. "
Draft Year: ".$row['CNDraftYR']." Draft Position: ".$row['CNDraftPOS']."
Drafted By: ".$row['DrTeam']." 
 
"; Print "
"; [/php] Here is the code that I used [php]if ($row['PositionID'] == "5") { $row = mysql_fetch_array($oGoalieNewStats); }elseif { $row = mysql_fetch_array($oPlayerStats); }; [/php] Here are the two quries that I use for the playerstats and goalienewstats [php] $oPlayerStats = mysql_query(" SELECT SeasonStats.Sea, SeasonStats.TeamID, SeasonStats.GP, SeasonStats.Goals, SeasonStats.Assists, SeasonStats.Points, SeasonStats.PlusMinus, SeasonStats.Pim, SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S, Seasonteam.Team, SeasonStats.SubSeason FROM SeasonStats Left JOIN Seasonteam On SeasonStats.TeamID=Seasonteam.TeamID WHERE SeasonStats.PlayerID=$iPlayerID ORDER BY SeasonStats.Sea ASC,SeasonStats.SubSeason ASC; ") or die(mysql_error()); Print ''; Print "

"; ECHO "
"; ECHO "
"; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; while($row = mysql_fetch_array($oPlayerStats)) { Print ""; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; } Print "
Season
Team
GP
G
A
P
PIM
+/-
PPG
SHG
GWG
GTG
Shots
".$row['Sea']."".$row['Team']."".$row['GP']."".$row['Goals']."".$row['Assists']."".$row['Points']."".$row['Pim']."".$row['PlusMinus']."".$row['PP']."".$row['SH']."".$row['GW']."".$row['GT']."".$row['S']."
"; $oPlayerStats = mysql_query(" SELECT SeasonStats.Sea, SeasonStats.TeamID, SeasonStats.GP, SeasonStats.Goals, SeasonStats.Assists, SeasonStats.Points, SeasonStats.PlusMinus, SeasonStats.Pim, SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S, Seasonteam.Team, SUM(SeasonStats.Goals) AS Goals_Total, SUM(SeasonStats.Assists) AS Assists_Total, SUM(SeasonStats.GP) As Games_Total, SUM(SeasonStats.Points) AS Points_Total, SUM(SeasonStats.Pim) AS Penalty_Total, SUM(SeasonStats.PlusMinus) AS PlusMinus_Total, SUM(SeasonStats.PP) AS Powerplay_Total, SUM(SeasonStats.SH) AS SH_Total, SUM(SeasonStats.GW) AS GW_Total, SUM(SeasonStats.GT) AS GT_Total, SUM(SeasonStats.S) AS Shots_Total FROM SeasonStats Left JOIN Seasonteam On SeasonStats.TeamID=Seasonteam.TeamID WHERE SeasonStats.PlayerID=$iPlayerID ORDER BY SeasonStats.Sea ") or die(mysql_error()); Print ""; ECHO "
"; ECHO "
"; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; while($row = mysql_fetch_array($oPlayerStats)) { Print ""; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; } Print "
Season
Team
GP
G
A
P
PIM
+/-
PPG
SHG
GWG
GTG
Shots
".$row['-']."".$row['-']."".$row['Games_Total']."".$row['Goals_Total']."".$row['Assists_Total']."".$row['Points_Total']."".$row['Penalty_Total']."".$row['PlusMinus_Total']."".$row['Powerplay_Total']."".$row['SH_Total']."".$row['GW_Total']."".$row['GT_Total']."".$row['Shots_Total']."
"; Print "
"; $oGoalieNewStats = mysql_query(" SELECT GoalieNewStats.Sea, GoalieNewStats.TeamID, GoalieNewStats.GP, GoalieNewStats.Min,GoalieNewStats.GAA, GoalieNewStats.W, GoalieNewStats.L, GoalieNewStats.OTL, GoalieNewStats.T, GoalieNewStats.SO, GoalieNewStats.GA, GoalieNewStats.SA, GoalieNewStats.PCT, GoalieNewStats.PIM, GoalieNewStats.Assists, Seasonteam.Team FROM GoalieNewStats Left JOIN Seasonteam On GoalieNewStats.TeamID=Seasonteam.TeamID WHERE GoalieNewStats.GoalieID=$iGoalieID ORDER BY GoalieNewStats.Sea, GoalieNewStats.SubSeason ASC; ") or die(mysql_error()); Print ''; Print "

"; ECHO "
"; ECHO "
"; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; ECHO ""; while($row = mysql_fetch_array($oGoalieNewStats)) { Print ""; Print " "; Print " "; Print " "; Print " "; $iGAA = $row['GA'] * 60 / $row['Min']; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; } Print "
Season
Team"; ECHO "
GP"; ECHO "
Min"; ECHO "
GAA"; ECHO "
W"; ECHO "
L"; ECHO "
OTL"; ECHO "
T"; ECHO "
SO"; ECHO "
GA"; ECHO "
SA"; ECHO "
PCT"; ECHO "
PIM"; ECHO "
Assists"; ECHO "
".$row['Sea']."".$row['Team']."".$row['GP']."".$row['Min']."".$english_format_number = number_format($iGAA, 2)."".$row['W']."".$row['L']."".$row['OTL']."".$row['T']."".$row['SO']."".$row['GA']."".$row['SA']."".$row['PCT']."".$row['PIM']."".$row['Assists']."
"; $oGoalieNewStats = mysql_query(" SELECT GoalieNewStats.Sea, GoalieNewStats.TeamID, SUM(GoalieNewStats.GP) AS GP_Total, SUM(GoalieNewStats.Min) AS Min_Total, SUM(GoalieNewStats.W) AS W_Total, SUM(GoalieNewStats.L) AS L_Total, SUM(GoalieNewStats.OTL) AS OTL_Total, SUM(GoalieNewStats.T) AS T_Total, SUM(GoalieNewStats.SO) AS SO_Total, SUM(GoalieNewStats.GA) AS GA_Total, SUM(GoalieNewStats.SA) AS SA_Total, GoalieNewStats.PCT, SUM(GoalieNewStats.PIM) AS PIM_Total, SUM(GoalieNewStats.Assists) AS Assists_Total, GoalieNewStats.SubSeason FROM GoalieNewStats Left JOIN Seasonteam On GoalieNewStats.TeamID=Seasonteam.TeamID WHERE GoalieNewStats.GoalieID=$iGoalieID ORDER BY GoalieNewStats.Sea ASC; ") or die(mysql_error()); Print ""; ECHO "
"; ECHO "
"; ECHO ""; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; while($row = mysql_fetch_array($oGoalieNewStats)) { Print ""; Print ""; Print ""; Print ""; Print ""; $iGAA = $row['GA_Total'] * 60 / $row['Min_Total']; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; Print " "; $iPCT = $row['SA_Total'] / ($row['SA_Total'] + $row['GA_Total']); Print " "; Print " "; Print " "; } [/php]
SeasonTeamGPMinGAAWLOTLTSOGASAPCTPIMAssists
".$row['-']."".$row['-']."".$row['GP_Total']."".$row['Min_Total']."".$english_format_number = number_format($iGAA, 2)."".$row['W_Total']."".$row['L_Total']."".$row['OTL_Total']."".$row['T_Total']."".$row['SO_Total']."".$row['GA_Total']."".$row['SA_Total']."".$english_format_number = number_format($iPCT, 3)."".$row['PIM_Total']."".$row['Assists_Total']."

That means there is an error in your code.
[php]error_reporting(E_ALL);
ini_set(‘display_errors’, 1);[/php]

Next. You really shouldn’t be using mysql_ functions.

Thanks for the advice, I put the error code into the php file but I still get a blank screen.

http://www.cnghl.org/cnghldb/cnghlplayerinfo.php?PlayerID=1

[php]<?PHP
error_reporting(E_ALL);
ini_set(‘display_errors’, 1);
// Connects to your Database
die(mysql_error());
mysql_select_db(“cnghldb”) or die(mysql_error());
//Get PlayerID from URL
$iPlayerID = $_GET[“PlayerID”];
$iNationID = $_GET[“NationID”];
$iPositionID= $_GET[“PositionID”];
[/php]

What should I be using as a replacement to mysql_functions?

I prefer PDO, but mysqli_ is another choice.

Is the code posted where the issue is? One option is to check your error_log. There may be a reference to what is happening. Post the code in its entirety for this page, http://www.cnghl.org/cnghldb/cnghlplayerinfo.php

Thanks,

I went with mysqli_ as It seemed the easier to go with at this point, but I will try out PDO once I get a little better at php. I have a new error and I did a search but I cannot seem to find the answer to this.

[php]
") or die(mysqli_connect_error());
$row = mysqli_fetch_array($oPlayerInfo);[/php]

I get this error

“Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/bjkirk47/public_html/cnghldb/cnghlplayerinfo.php on line 40”

The snippet above in php is row 40 and 41

Here is what the page looks like so far

http://www.cnghl.org/cnghldb/cnghlplayerinfo.php?PlayerID=1

mysqli_fetch_array

Wrong one,

mysqli_query()

I made the change and I still get the error

mysqli_query() expects at least 2 parameters, 1 given in /home/bjkirk47/public_html/cnghldb/cnghlplayerinfo.php on line 40

[php]
") or die(mysqli_connect_error());
$row = mysqli_query($oPlayerInfo);
[/php]

Above are lines 40,41

You didn’t make the changes. Look at the documentation examples for the procedural usage.

You can’t just add an “i” to MySQL and expect it to work. There are differences. See the manual for examples.

CNGHL,
If you are comfortable with MySQL_ functions, I would stick with them and just upgrade your code to the
newer “Improved” version MySQLi_ functions. PDO would take you longer to learn, although a lot of the
experienced techs that write on this site say that it is the way to go.

To upgrade to MySQLi versions of the functions, there are only a few changes you need to make. One of the
most important ones is to add a pointer in your query functions to point to the actual connection your created.

Here is a great and simple tutorial that should help explain it. This first link is an overview of all of the
funcitons. The second is the first step in updating. The connection to the database. The last is how to
run a query. As you will see in all of these, the change is very minor and you should be able to make the
needed changes in very little time.
http://www.w3schools.com/php/php_ref_mysqli.asp
http://www.w3schools.com/php/func_mysqli_connect.asp
http://www.w3schools.com/php/func_mysqli_query.asp
As you will see, the changes to your code for the improved versions will be adding the “i” and in most of
your code, adding in the connection variable. Very quick and easy. Then, take a peek into PDO at some
time.

Hope that helps!

Sponsor our Newsletter | Privacy Policy | Terms of Service