Echo data from two different mysql tables using php - php

I'm trying to display data from two different mysql tables on my website using php.
My aim is to display a match schedule with information from two tables. The first one includes all the teams plus their ID, the second includes all details on the games. I now want to "replace" the "home_id" and "away_id" fields with the respective team names from the first table.
The tables look as follows:
table "teams"
id name
-----------
1 Team 1
2 Team 2
3 Team 3
4 Team 4
...
table "matchschedule"
id home_id away_id goals_home goals_away date
1 1 2 0 2 2016-05-05
2 3 4 2 1 2016-05-06
...
With the following query I'm getting the required data within phpmyadmin:
SELECT
date, home.name, sp.goals_home, away.name, sp.goals_away
FROM
matchschedule sp
INNER JOIN
teams home on sp.home = home.id
LEFT JOIN
teams away on sp.away = away.id
However, when I implement this query into my website and add the code below to display the data the fields "home.name" and "away.name" are always empty. What do I need to change in order to get the team names displayed?
while($row = mysql_fetch_array($query)) {
?>
<table border=1>
<tr>
<?php
echo "<td>".$row['date']."</td>";
echo "<td>".$row['home.name']."</td>";
echo "<td>".$row['goals_home']."</td>";
echo "<td>".$row['away.name']."</td>";
echo "<td>".$row['goals_away']."</td>";
?>
</tr>
</table>
Final result (with missing info for both team names):
2016-05-05 [] 0 [] 2
2016-05-06 [] 2 [] 1

The issue is that when in query you request home.name mysql return to php column with title name and you can not reach that column using home.name anymore. And same for away.name.
That's why you need to set proper name for returned columns.
Change your query to:
SELECT
date, home.name home_name, sp.goals_home, away.name away_name, sp.goals_away
FROM
matchschedule sp
INNER JOIN
teams home on sp.home = home.id
LEFT JOIN
teams away on sp.away = away.id
and call those columns in php like:
echo "<td>".$row['date']."</td>";
echo "<td>".$row['home_name']."</td>";
echo "<td>".$row['goals_home']."</td>";
echo "<td>".$row['away_name']."</td>";
echo "<td>".$row['goals_away']."</td>";

Based on your table strcture, you should do the following :
SELECT
date, home.name AS homeName, sp.goals_home, away.name AS awayName, sp.goals_away
FROM
matchschedule sp
INNER JOIN
teams home on sp.home_id = home.id
LEFT JOIN
teams away on sp.away_id = away.id
So basically replace sp.home by sp.home_id and sp.away by sp.away_id and change the fields name as you have the same fields name in your tables.

Related

The Mysql INNER JOIN fetch same data repeatly

I try to design a web that keep track the match history of students.
tablegame has 4 column: player 1, player2 ,player 3 and player4. All of them are represented as ids in tablegame.(each game has a maximum of 4 player)
The column player1 player2 always store the IDs who win this game, and column player 3 player4 always store IDs who lose this game.
Tableplayer store basic information about the student.
However, as I fetch associated array from the $AimStudentHistoryWin. Data get repeatly fetched.
THE web actually shows me this:
================
GAMEID 15
================
GAMEID 16
================
GAMEID 15
================
GAMEID 16
================
GAMEID: 15
================
GAMEID 16
BUT I expect the web to only show me
================ GAMEID 15
================ GAMEID 16
How could I Fix that?
echo "<br><br> <center><h2>match history:</h2> <br>";
$Aimstudenthistorywin=$conn ->query( "SELECT tablegame.*,tableplayer.Name FROM tablegame INNER JOIN tableplayer ON tablegame.player1 ='$studentid' OR tablegame.player2 ='$studentid'");
$Aimstudenthistoryloss=$conn ->query("SELECT tablegame.*,tableplayer.Name FROM tablegame INNER JOIN tableplayer ON tablegame.player3 ='$studentid'OR tablegame.player4 ='$studentid'" );
$Num=0;
While($row2= $Aimstudenthistorywin ->fetch_assoc()){
echo "<br>================<br><i style ='color:blue;'><h3>WIN</h3></i>";
echo "<br>Game".$Num;
$Num++;
echo "<br>".$row2['GameID'].":<br>".$row2['Name'];
}while($row2 = $Aimstudenthistoryloss -> fetch_assoc()){
echo "<br>================<br><i style ='color:red;'><h3>Lose</h3></i>";
echo "<br>Game".$Num;
$Num++;
echo "<h3>StudentName:".$TempStudentArray[$row2['player1']]."</h3><br>Game Date:".$row2['Date']."<br><br>"."----------------<br>Player1:".$TempStudentArray[$row2['player1']]."|||||||"."Player2:".$TempStudentArray[$row2['player2']]."<br><br>vs<br><br>Player3:".$TempStudentArray[$row2['player3']]."|||||||| Player4".$TempStudentArray[$row2['player4']]."<br>-------" ;
}
echo"</center>"
Ok it is solved.
instead of using
"SELECT tablegame.*,tableplayer.Name FROM tablegame INNER JOIN tableplayer ON tablegame.player1 ='$studentid' OR tablegame.player2 ='$studentid'");
We can just add more conditions into this. Eventhough I don't know why it works but it works
"SELECT tablegame.*,tableplayer.Name FROM tablegame INNER JOIN tableplayer ON (tableplayer.StudentID = tablegame.player1 OR tableplayer.StudentID = tablegame.player2) AND (tablegame.player1 ='$studentid' OR tablegame.player2 ='$studentid')");
Your duplicates come from your join's ON clause, which basically acts as if it was a WHERE clause.
To fix it, you can properly JOIN both tables (using a column of each), and then selecting only those involving your specific student (in the WHERE clause):
SELECT tablegame.*,tableplayer.Name
FROM tablegame
INNER JOIN tableplayer
ON tablegame.player1 = tableplayer.StudentID
OR tablegame.player2 = tableplayer.StudentID
WHERE tableplayer.StudentID = [the id of your student]
This request will avoid duplicates (assuming a student can't be player 1 and 2 at the same time)
And while at it, I would suggest always using prepared statements, to avoid potential SQL injections.

Count from selection and access both tables with joins

I stumbled upon a query that I have never done until now.
Before asking the question I looked for if another user had had the same need as me but nothing.
My goal is very simple:
having two tables:
collaboratori (collaborators)
invite (invitations)
I have to count how many invitations the collaborators have made.
table structure of collaboratori:
ID_Collaboratori | cod_manager
37 4675
150 6675
3 6575
table structure of inviti:
invite_id | invite_code_manager
37 6675
39 6575
40 4675
41 6675
if I execute the join obviously I access the two tables in this way:
$q_stats_prod_manager = $connessione->prepare("
SELECT * FROM invite
LEFT JOIN collaboratori
ON collaboratori.cod_manager = invite.invite_code_manager ");
$q_stats_prod_manager->execute();
$r_stats_prod_manager = $q_stats_prod_manager->get_result();
my need lies in showing in a table:
show me for each manager who has his cod_manager inside the inviti table, the number of times he sent them.
Name Surname Manager 1 | Number of invite: 200
Name Surname Manager 2 | Number of invite: 50
Name Surname Manager 3 | Number of invite: 10
not limiting myself to just one counter but also being able to access other table values ​​like any join
I take the liberty of putting the answer that was partially written by another user, adding a detail and explanation for future users. The resolution query for this case is the same:
$q_stats_prod_manager = $connessione->prepare("
SELECT count(invite.invite_id)
/*name of what you want to call the result you will see in the while*/
AS result_count, /*you can call this value whatever you want*/
/*Start | Values ​​of the tables you are interested in selecting*/
collaboratori.nome,
collaboratori.data_registrazione,
invite.invite_code_manager
/*End | Values ​​of the tables you are interested in selecting*/
FROM collaboratori
LEFT JOIN invite
ON invite.invite_code_manager = collaboratori.cod_manager group by invite.invite_code_manager
");
$q_stats_prod_manager->execute();
$r_stats_prod_manager = $q_stats_prod_manager->get_result();
$count_invite_manager=mysqli_fetch_array($r_stats_prod_manager);
$number_of_invite_manager = $count_invite_manager[0];
Select the id of the table you want to count
Give a name you wish you want to name the counted result
Select the values ​​of the tables on which you will perform the join you want to view
Join the tables
Show the result with while
Code while:
<?php while($rowstatspm = mysqli_fetch_assoc($r_stats_prod_manager)){ ?>
<!-- this is the fancy name you associated with your query when you wrote: AS nameofwhatyouwant -->
<?php echo $rowstatspm['result_count'] ;?>
<?php } ?>
You seem to want aggregation. I assume you want all rows for collaboratori, so that should be the first table for the LEFT JOIN:
SELECT c.cod_manager, COUNT(i.invite_code_manager)
FROM collaboratori c LEFT JOIN
invite i
ON c.cod_manager = i.invite_code_manager
GROUP BY c.cod_manager;
Your question doesn't describe where the name comes from. But those fields should be in both the SELECT and GROUP BY.
SELECT count(invite.invite_id),collaboratori.name FROM collaboratori
LEFT JOIN invite
ON invite.invite_code_manager = collaboratori.cod_manager group by invite.invite_code_manager

Select from three tables

I have three tables where table_2 is the middle(connected) between table_1 and table_3
tables
table_id
...
...
table_rest
rest_id
table_id
...
rest
rest_id
...
...
And the query to select I use
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
WHERE rest_id = '$rest_id'
What I need now is to join in this query another table reserv
id
...
status
To check if status is 0, 1,or 2 to not show me anything if there is no status this mean there is no record to show me. In other words this is resserved system where I show on screen few tables. If status is 0,1,2 thats mean the table is taken. If nothing is found for status this mean that there is no record for table and can be shown to user.
EDIT: Sample scenario
tables
table_id
1
2
3
4
5
rest
rest_id
1
2
table_rest
table_id | rest_id
1 2
2 2
3 2
4 2
5 2
So the query that is above will generate 5 tables for rest_id=2 and none for rest_id=1
So now I have another table
reserv
id | status
1 0
2 1
3 2
So in this table reserv currently are saved 3 tables. The idea is to show me other two whit id=4 and id=5 because they are not in table reserv and don't have any status.
Hope is a little bit more clear now.
You have to point from table reserv to which table is beign booked, let's call it reserv.table_id
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
left join reserv
on reserv.table_id = m.id
WHERE rest_id = '$rest_id'
and reserv.status is null (*note)
*note use 'is' or 'is not' depending of your needs, as far as I read, first seems that you want !=, later that what you want is =
It's better if you provide sample data or sqlfiddle. Based on what I realize: Is this what you want:
select tables.table_id, rest.rest_id
from tables
left join table_rest on table_rest.table_id = tables.table_id
left join rest on rest.rest_id = table_rest.rest_id
where rest.rest_id = '$rest_id'
and tables.table_id not in (select id from reserv)

Join 2 tables together in one query statement php

Im trying to join two tables together becouse 2 columns match and i need info from second table to display the content. When i pressing a link with ?p=1a i want content to show and this info i have on the second table but not the first one. Where table 1 and table 2 match on column Menu. I have shorten down on some code/table info becouse its not relevant for this problem. im then displaying the info with mysql_fetch_assoc.
TABLE 1
MENU | subtitle | firstname |info
info | contact
word | woord
TABLE 2
MENU | page |
info | 1a
word | 1b
My code:
if(isset($_GET['p'])){
$page = $_GET['p'];
$find = mysqli_query("SELECT * FROM testcheck, testdoc INNER JOIN testdoc ON testcheck.Menu = testdoc.MENU AND page='$page' ");
while($row = mysqli_fetch_assoc($find)){
$subtitle = $row['subtitle'];
$firstname = $row['firstname'];
echo $firstname
}
}
Problem is correct kinda now but only letters work fine but when i combine page='1a' for example everything stop works.
Your syntax for the join is wrong.
Use:
"SELECT * FROM testcheck
INNER JOIN testdoc ON testcheck.Menu = testdoc.MENU AND page='$page'
");
Read more about LEFT JOIN and RIGHT JOIN. In your case you would need RIGHT JOIN to get data that is in second table but not in first.
Like popovitsj suggested your syntax is also wrong.
Correct syntax:
SELECT * FROM testcheck
INNER JOIN testdoc ON testcheck.Menu = testdoc.Menu AND page='$page'
Also note that you are using MENU upper case which is not a problem on windows but will be a problem on unix, it will give you an error Column Not Found.
EDIT
If your table has column MENU then it should be fine.

Count the number of rows in MySQL table with criteria depending on other table

I've got two tables:
content:
id access
1 3
2 5
3 9
viewlevels:
id group
1 [10,12,15]
2 [8,12,11]
3 [9,10,5]
The access field in content is related with the id field in viewlevels.
I select the rows in viewlevels depending on the current user group. So for example, if group is = 10, my query will select rows 1 and 3. If the group is 12, it will select rows 1 and 2, etc. I'm using the following query:
$query="SELECT id FROM #__viewlevels WHERE rules LIKE '%$group%'";
My challenge is to count the number of rows for column id in table content where the access matches with the selected id's from the above query on table viewlevels.
I tried the following code but it is returning the error: undefined variable: nartigos
$query="SELECT count(id) FROM #__content WHERE access IN (SELECT id FROM #__viewlevels WHERE rules LIKE '%$group%')";
if ($stmt = mysqli_prepare($link, $query)) {
/* execute query */
mysqli_stmt_execute($stmt);
/* store result */
mysqli_stmt_store_result($stmt);
$nartigos=mysqli_stmt_num_rows($stmt);
/* close statement */
mysqli_stmt_close($stmt);
};
echo "Nº de artigos " .$nartigos;
First of all, you really should normalize your data. Consider having a many-to-many join table for viewLevels instead of having all groups in one row. That might look like this:
access_id group_id
1 10
1 12
1 15
2 8
2 11
2 12
3 5
3 9
3 10
That would make your query as simple as
SELECT c.id AS `content_id`, COUNT(v.access_id) AS `content_count`
FROM content AS c INNER JOIN viewLevels AS v
ON c.access_id = v.access_id
WHERE v.group_id = ?
GROUP BY c.id
Here ? is the group id you are querying against.
Without normalization (which again I STRONGLY recommend you do), you would still use a join, but it would look like this:
SELECT c.id AS `content_id`, COUNT(v.access_id) AS `content_count`
FROM content AS c INNER JOIN viewLevels AS v
ON c.access_id = v.access_id
WHERE v.group LIKE '%?%'
GROUP BY c.id
you need to "join" your tables.
the sql command cant query two tables seperately.
when you "join" 2 tables in your sql, think of it as making one virtual/temporary table in the air, of the 2 tables which you can then query.
this is quite a good intro http://www.sitepoint.com/understanding-sql-joins-mysql-database/

Categories