I have connected FirstPositionID/SecondPositionID with PositionID(PRIMARY KEY) from positions. Each positionID has one position name. Since i have 2 positions for players setted by position id, shouldn't php print position name of each id i have set? When i print PositionName it prints all positions i have inserted in my positions table. here is my tables
CREATE TABLE `players` (
`PlayerID` int(10) UNSIGNED NOT NULL,
`PlayerName` varchar(255) NOT NULL,
`CountryID` varchar(255) NOT NULL,
`FirstPositionID` int(11) UNSIGNED NOT NULL,
`SecondPositionID` int(10) UNSIGNED NOT NULL,
`Overall` int(11) UNSIGNED NOT NULL,
`Defence` int(11) NOT NULL,
`Speed` int(11) NOT NULL,
`Rebound` int(11) NOT NULL,
`Stamina` int(11) NOT NULL,
`TeamID` int(11) NOT NULL,
`Shooting` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16;
CREATE TABLE `positions` (
`PositionID` int(10) UNSIGNED NOT NULL,
`PositionName` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16;
//-----------php-----------------------
$sql= "SELECT * from players,positions";
$reg = mysqli_query($con,$sql);
if(mysqli_num_rows($reg) > 0)
{
while($row = mysqli_fetch_assoc($reg))
{
echo "</br>Player:".$row['PlayerName']." Overall:".$row['Overall']." Position: ".$row['PositionName'];
}
}
else
{
echo "SQL error at: </br> ".$register."SYNTAX:</br>".mysqli_error($con)."</br>";
}
RESULT:
Player:Agrabanis Overall:64 Position: Point Guard
Player:Athineou Overall:64 Position: Point Guard
Player:Agrabanis Overall:64 Position: Shooting Guard
Player:Athineou Overall:64 Position: Shooting Guard
Player:Agrabanis Overall:64 Position: Small Forward
Player:Athineou Overall:64 Position: Small Forward
Player:Agrabanis Overall:64 Position: Power Forward
Player:Athineou Overall:64 Position: Power Forward
Player:Agrabanis Overall:64 Position: Center
Player:Athineou Overall:64 Position: Center
Since i have set to firstplayer position ids 5,4 and to the secondplayer position ids 2,1 shouldnt just print Center,Power Forward(4,5 position ids) for first player and for secondplayer Shooting Guard,Point Guard(2,1 position ids)
please use left join
or inner join
instead of cross join and change your SQL string to something like
SELECT pl.PlayerName, pl.FirstPositionID, pl.Overall,
pl.SecondPositionID, po1.PositionName AS FirstPositionName,
po2.PositionName AS SecondPositionName FROM players pl
LEFT JOIN positions po1 ON(pl.FirstPositionID = po1.PositionID )
LEFT JOIN positions po2 ON (pl.SecondPositionID = po2.PositionID )
LIMIT 0,100
Also, change you while loop to
while($row = mysqli_fetch_assoc($reg)){
echo "</br>Player:".$row['PlayerName']." Overall:".$row['Overall']." Position1: ".$row['FirstPositionName']." Position2:".$row['SecondPositionName '];
}
I think this will helps you...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.