I have 2 tables, in 1 table there are lists of all the episodes from a movie and in the other table there are the streams from the episode. Every episode got his own id so the streams are linked via the id from the episode.
But now I have a problem: I want to make a episode-list and at the same time I want to insert a link to the stream. So that means that I have to fetch 2 tables at the same time but I don't know how. This is my code
<?php
include 'connection.php';
$a = (int)$_GET['a'];
if ($result = $con->prepare("SELECT id, ep_nr, ep_title FROM anime_episode WHERE ani_id = $a"))
{
$result->execute();
$result->bind_result($eid, $nr, $title);
while ($result2 = $con->prepare("SELECT * FROM anime_stream WHERE ep_id = $eid"))
{
$result2->execute();
$result2->bind_result($eid, $etitle, $lang, $sname, $link, $uploadedby);
echo '<div id="list-box">';
echo '<table cellspacing="0">';
while ($result->fetch() && $result2->fetch())
{
echo '<tr>';
echo '<td width="50">' . $nr . '</td>' . '<td>' . $title . '</td>' . '<td>' . $link. '</td>';
echo '</tr>';
}
echo '</div>';
echo '</table>';
}
}
$con->close();
?>
As Strawberry suggests, I believe your looking to join the two table based on the foreign key and select the content of both tables, somethig like...
select
epi.*,
str.*
from
anime-episode epi
inner join
anime-stream str on epi.ani-id = str.epi-id
where
epi.ani-id = $id
This assumes that your episodes will always have at least one stream as an inner join will only return a row (with the column of both tables) when a record is found in each of the tables of the join. A Left Outer join can be used which will still return movies even if no streams exist for a given episode.
Can't find underline on my tablet!
A query something like this should do the trick.
SELECT ae.id, ae.ep_nr, ae.ep_title, as.* FROM anime_episode ae
LEFT JOIN anime_stream as ON as.ep_id = ae.id
WHERE ae.ani_id = [$arg]
Related
how can I display the name of both teams (lteam and vteam)? Query works now... This is a screenshot of the SQL results:
http://prntscr.com/f9mkqh
$sql = "
SELECT *
FROM fixtures
LEFT
JOIN teams AS a
ON fixtures.lteam = a.id
LEFT
JOIN teams AS b
ON fixtures.vteam = b.id
WHERE date_ko = '2017-05-19'
";
echo '<table>';
echo '<tbody>';
foreach($pdo->query($sql) as $row)
{
echo '<tr>';
echo '<td>' . $row['lteam'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>-</td>';
echo '<td>' . $row['vteam'] . '</td>';
echo '<td>' . $row['b.name'] . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
Thanks!
The SQL result header is somewhat confusing, and as I don't know fully how the tables and schemas are structured, I have to assume you are having trouble with getting the name attribute from the teams table, as this is joined in two times.
You will need to select the columns explicitly and naming them something else for this to work. For example:
SELECT *, `a`.`name` as `team1name`, `b`.`name` as `team2name` FROM fixtures [...]
Now you should be able to grab the team names under their assigned aliases.
I am new to PHP and I just cannot figure out my code. I am using MySQL and PHP.
table: person
PK: personID
Other fields: lastName, firstName, hireDate, imgName
table: validMajors
PK: majorAbbrev
Other Fields: majorDesc
(Junction) table: personMajors
personID, majorAbbrev
When I run my code (using NATURAL JOIN) it will display the image, last&first name, and hire date. Which is great! But I need it to display their majors as well (I would like the majorAbbrev to be displayed). It also does not display people who are in the person table but are not in the personMajors table, which is an issue because we have staff members in the person table (who do not have a major since they are not a student)
Here is my code:
<table align="center">
<?php
$connection = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
if ( mysqli_connect_errno() ) {
die( mysqli_connect_error() );
}
$sql = "SELECT * FROM person NATURAL JOIN personMajors ORDER BY lastName";
if ($result = mysqli_query($connection, $sql)) {
// loop through the data
$columns=4;
$i = 0;
while($row = mysqli_fetch_assoc($result))
{
if($i % $columns ==0){
echo "<tr>";
}
echo "<td class='staffImage badgeText frameImage displayInLine'>" . "<img src='images/staff/".$row['imgName'].".jpg'>". "<br>".
"<strong>" . $row['firstName'] . "</strong>" ." ".
"<strong>" . $row['lastName'] . "</strong>" . "<br>" .
"Hire Date: ".$row['hireDate'] ."</td>";
"Major: " .$row['majorAbbrev'] ."</td>"; //Does not display
if($i % $columns == ($columns - 1)){
echo "</tr>";
}
$i++;
}
// release the memory used by the result set
mysqli_free_result($result);
}
// close the database connection
mysqli_close($connection);
?>
</table>
Any ideas/solution will be greatly appreciated!
Because you are not concatenating your php properly. You ended (;) your echo after displaying the $row["lastName"].
You can try these to join the three tables:
SELECT * FROM person
LEFT JOIN personMajors ON person.personID = personMajors.personID
LEFT JOIN validMajors ON personMajors.majorAbbrev = validMajors.majorAbbrev
Or you can define what columns to call in your query:
SELECT person.personID,
person.lastName,
person.firstName,
person.hireDate,
person.imgName,
validMajors.majorAbbrev,
validMajors.majorDesc
FROM person
LEFT JOIN personMajors ON person.personID = personMajors.personID
LEFT JOIN validMajors ON personMajors.majorAbbrev = validMajors.majorAbbrev
Then you can call the results with the way you are calling it right now (cleaner version):
echo '<td class="staffImage badgeText frameImage displayInLine">
<img src="images/staff/'.$row["imgName"].'.jpg"><br>
<strong>'.$row["firstName"].'</strong>
<strong>'.$row["lastName"].'</strong><br>
Hire Date: '.$row["hireDate"].'
Major: '.$row["majorAbbrev"].'
</td>';
(Second try): Is the person to major relationship one to one or one to many?
OK, this SELECT Statement should work:
SELECT person.*, validMajors.* FROM person AS p, validMajors AS vm, personMajors AS pm WHERE p.personID = pm.personID AND pm.majorAbbrev = vm.majorAbbrev
I have 2 table bpi_registration and bpi_teamProfile. The fields in bpi_registration are : id, id_school, first_name,last_name,email,city,state,country and fields in bpi_teamProfile table are id_team, team_name, id_student1, id_student2, id_student3 now id_student1, id_student2, id_student3 contains the same id of students that were in bpi_registration. I am not sure how to connect multiple fields. The query that i have written is below. Please correct me if i am wrong:
SELECT * FROM bpi_registration
INNER JOIN bpi_teamProfile
ON bpi_registration.id=bpi_teamProfile.id_student1
AND bpi_registration.id=bpi_teamProfile.id_student2
AND bpi_registration.id=bpi_teamProfile.id_student3
AND bpi_registration.id=bpi_teamProfile.id_student4
AND bpi_registration.id=bpi_teamProfile.id_student5
I am trying to implement search filter in such a way that when someone clicks on Team dropdown then the firstname,lastname,email,city,state,country from bpi_registrationshows up. Below is my PHP code
if (isset($_GET['Team']))
{
$sql="SELECT * FROM bpi_registration
INNER JOIN bpi_teamProfile
ON bpi_registration.id=bpi_teamProfile.id_student1
AND bpi_registration.id=bpi_teamProfile.id_student2
AND bpi_registration.id=bpi_teamProfile.id_student3
AND bpi_registration.id=bpi_teamProfile.id_student4
AND bpi_registration.id=bpi_teamProfile.id_student5"
$userQuery = "{$sql} WHERE bpi_teamProfile.team_name = :team_id";
$user = $db->prepare($userQuery);
$user->execute(['team_id' => $_GET['Team']]);
$selectedUser=$user->fetch(PDO::FETCH_ASSOC);
if(isset($selectedUser))
{
echo '<tr>';
echo '<td>' . $selectedUser['first_name'] . '</td>';
echo '<td>' . $selectedUser['last_name'] . '</td>';
echo '<td>' . $selectedUser['email'] . '</td>';
echo '<td>' . $selectedUser['address_city'] . '</td>';
echo '<td>' . $selectedUser['address_state'] . '</td>';
echo '<td>' . $selectedUser['address_country'] . '</td>';
echo '</tr>';
}
}
The URL looks when we click on team filter looks like this - https://www.example.com/retrieve1.php?Grade=&School=&Team=mary+winners&Students=
However i am not able to get the desired result.
It seems unlikely the student id columns in a given row all map back to the same id (in registration)...or the query would be returning properly. If they are different, do want to 'OR' the join?
SELECT * FROM bpi_registration
INNER JOIN bpi_teamProfile
ON (bpi_registration.id=bpi_teamProfile.id_student1)
OR (bpi_registration.id=bpi_teamProfile.id_student2)
OR (bpi_registration.id=bpi_teamProfile.id_student3)
OR (bpi_registration.id=bpi_teamProfile.id_student4)
OR (bpi_registration.id=bpi_teamProfile.id_student5)
I have two databases and tables in each. Am reading the renewal_date of DB1 table 1 and taking the renewal_date of the current month and domain_name for that record.
then am trying to retrieve the d_due_date from DB2 table2 for the domain_name selected from DB1 table1.
then i need to display domain_name, renewal_date,d_due_date in one table.
I can do this by joining the database with INNER JOIN.
what i need is to write separate select queries and display.
$sql = "select domain_name from table1 where MONTH(renewal_date) = '06'";
$result = mysqli_query($link_id,$sql);
if(!$result) die(sql_error());
$DoNM= Array();
$sql1= "select d_due_date from domains where d_domain IN ('abc.com','akaaasa.com')";
$result1 = mysqli_query($link_id1,$sql1);
if(!$result1) die(sql_error());
$DoNM1= Array();
echo '<table>';
while(($row1 = mysqli_fetch_array($result1,MYSQL_ASSOC))&&($row = mysqli_fetch_array($result,MYSQL_ASSOC))){
echo "<tr>";
echo "<td>" .$DoNM[]= $row['domain_name'] . "</td>";
echo "<td>" .$DoNM[]= $row['renewal_date'] . "</td>";
echo "<td>" .$DoNM1[]= $row1['d_due_date'] . "</td>";
echo "</tr>";
}
echo '</table><br />';
I have hardcoded the domain name in $sql1. what I want is to get that from $sql. how can I do that.
So all you need to do is process through the first query results and build the array, then convert the contents of the array to a comma delimited list
$sql = "select domain_name, renewal_date
from table1
where MONTH(renewal_date) = '06'";
$result = mysqli_query($link_id,$sql);
if(!$result) die(sql_error());
$db1= Array();
$InList = '';
while( $row = mysqli_fetch_array($result,MYSQL_ASSOC) ) {
$InList .= sprintf("'%s',", $row['domain_name']);
$db1[$row['domain_name']] = $row['renewal_date'];
}
$InList = rtrim($InList, ',');
$sql = "select d_due_date, d_name
from domains
where d_domain IN ($InList)";
$result = mysqli_query($link_id1,$sql);
if(!$result) die(sql_error());
echo '<table>';
while( $row = mysqli_fetch_array($result,MYSQL_ASSOC ){
echo '<tr>';
echo '<td>' . $row['d_name'] . '</td>';
// find the db1.renewal_date matching d_name from db1 array
echo '<td>' . $db1[$row['d_name']] . "</td>";
echo '<td>' . $row['d_due_date'] . '</td>';
echo "</tr>";
}
echo '</table><br />';
RE: Your comment
So now I have saved the data from db1 into an array you can use the get the db1.renewal_date from in the output phase. Also I added the db1.d_name to the second query so you have the key to the array containing the db1.renewal_date
RE: Using more fields from table1:
Sure, thats not a problem. This will mean that you have to store an array i.e. the $row as the data so you have the complete set of columns saved in the $db1 array.
$sql = "select domain_name, renewal_date, f3, f4
from table1
where MONTH(renewal_date) = '06'";
$result = mysqli_query($link_id,$sql);
if(!$result) die(sql_error());
$db1= Array();
$InList = '';
while( $row = mysqli_fetch_array($result,MYSQL_ASSOC) ) {
$InList .= sprintf("'%s',", $row['domain_name']);
$db1[$row['domain_name']] = $row;
}
$InList = rtrim($InList, ',');
The $db1 array will now look like this:
Array
(
[abc.com] => Array
(
[domain_name] => abc.com
[renewal_date] => 2015-06-06
[f3] => aaa
[f4] => bbb
)
[xyz.com] => Array
(
[domain_name] => xyz.com
[renewal_date] => 2015-06-07
[f3] => ccc
[f4] => ddd
)
)
So the domain name is still the KEY to each occurance of the array, but you have another array associated with the key rather than just a single string.
So to access this array you do this to use a domains specific columns.
echo '<td>' . $db1[ $row['d_name'] ] ['renewal_date'] . "</td>";
echo '<td>' . $db1[ $row['d_name'] ] ['f3'] . "</td>";
echo '<td>' . $db1[ $row['d_name'] ] ['f4'] . "</td>";
I hope that is explained well enough to help you.
First of all, you can do this in just one database with multiple tables which would be nicer and easier to use than you should be able to do something like:
SELECT domain_name,renewal_date,d_due_date
FROM table1 INNER JOIN table2
ON table2.d_name = table1.domain_name
WHERE MONTH(table1.renewal_date) = '06'";
Something like this(also see this as referance)
using where and inner join in mysql
I think that due to use of && operator in while loop. There may be one of the table's result is empty and that's why the result set may be empty.
I have my MySQL data output into a HTML table successfully and exactly how I want it. Now I'd like to filter this data based on multiple dropdown values and a submit button. I've looked around in tutorials and other questions but can't find what I'm looking for. Here's how I output:
$fetchResult = "SELECT a.MembershipID, a.FirstName, a.Surname, t.RaceID, r.RaceName, t.Time, r.RaceID, r.ClubYear
FROM Athlete AS a
INNER JOIN Time AS t
ON a.MembershipID=t.MembershipID
INNER JOIN Race AS r
ON t.RaceID=r.RaceID
ORDER BY a.Surname";
$result = $mysqli->query($fetchResult);
//Start table
echo "<table>";
echo "<tr>
<th>First Name</th>
<th>Surname</th>
<th>Race</th>
<th>Time</th>
<th>Club Year</th>";
// Loop through database
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $row['FirstName'] . '</td>';
echo '<td>' . $row['Surname'] . '</td>';
echo '<td>' . $row['RaceName'] . '</td>';
echo '<td>' . $row['Time'] . '</td>';
echo '<td>' . $row['ClubYear'] . '</td>';
}
echo "</tr></table>";
Now I have 4 dropdowns also populated by data from the database. What I'd like to happen is, the user picks an option between 1 and 4 dropdowns and the data in the HTML gets filtered but I can't get this to work. Here is what I have so far:
if (!empty($clubYear)) {
$fetchResult = "SELECT a.MembershipID, a.FirstName, a.Surname, t.RaceID, r.RaceName, t.Time, r.RaceID, r.ClubYear
FROM Athlete AS a
INNER JOIN Time AS t
ON a.MembershipID=t.MembershipID
INNER JOIN Race AS r
ON t.RaceID=r.RaceID
WHERE r.ClubYear='$clubYear'
ORDER BY a.Surname";
}
$result = $mysqli->query($fetchResult);
So I check to see if the dropdown is empty, if not, apply a query with a WHERE filter. This just makes the page refresh and go to a "no data received" page. Where am I going wrong? The filter should be applied based on what's been selected in the dropdown but without explicitly stating what that dropdown equals i.e= if $clubYear == "whatever", then WHERE r.ClubYear="whatever" - I'd like to just filter through passing the variable.
Thanks.