Selecting multiple tables PHP - php

I'm having issues with this code, it's displaying multiple of the same table headings and I don't know why.
Heres the code :
#This section of code will display my affected_countries table
echo"<br><br>This will list and display important information about the statistics of the infections<br>";
$sql = "SELECT * from affected_countries,infection_info";
$result = mysqli_query($db, $sql);
echo"<table border='1'><tbody>";
echo"<tr><td><b>Name</b></td><td><b>Country Origin</b></td><td><b>Number of Countries affected</b></td></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo"<tr><td>{$row['NAME']}</td><td>{$row['Country Origin']}</td><td>{$row['Number of Countries affected']}</td></tr>";
}
echo"</tbody></table><br>";
Output on website:
Two Tables above
Table below(Issue)
It just has multiple of the same thing and I don't know what I should do
Any help is appreciated

You have to use SQL joins to get columns from multiple tables.
See the example query:
SELECT ac.name, ii.country_origin, ii.number_of_countries_affected FROM affected_countries ac INNER JOIN infection_info ON ac.name =ii.name;

You should list from two tables separately
$sqlAffected = "SELECT * from affected_countries";
$sqlinfection_info = "SELECT * from infection_info";
$resultA = mysqli_query($db, $sqlAffected);
$resultB = mysqli_query($db, $sqlinfection_info);
or something like this:
SELECT * FROM affected_countries UNION infection_info;
or
SELECT * FROM affected_countries LEFT JOIN infection_info on afftected_countries.primary_key = infection_info.affected_country_id;

Related

Multiple Table Search and show the result

I was trying to fetch data from multiple tables in the Database and to Fetch data I tried using UNION ALL ... but I wasn't able to work on it.
$sqll = "SELECT * FROM msr_bills WHERE mobile='94825XXXX' UNION ALL
SELECT * FROM hirisave_bills WHERE mobile='94825XXXX'";
$sql = mysqli_query($conn,$sqll);
while($row = mysqli_fetch_array($sql)){
echo $row['name'];
echo $row['mobile'];
}
I am getting this error:
The used SELECT statements have a different number of columns
Maybe try to use LEFT JOIN like this:
$sqll = "SELECT * FROM `msr_bills` mb LEFT JOIN `hirisave_bills` hb ON hb.`mobile` = mb.`mobile` WHERE mb.`mobile` = '94825XXXX'";
You have all columns from two tables or nulls from hirisave_bills if there is no such mobile number.
you have diferent number of columns in your tables. try to select same number of columns
For example:
$sqll = "SELECT id, mobile FROM msr_bills WHERE mobile='94825XXXX' UNION ALL SELECT id, mobile FROM hirisave_bills WHERE mobile='94825XXXX'";

PHP Retrieve information from five different tables with correct order

I develop a chat system where students and staff can exchange different messages. I have developed a database where we have five tables: the staff table, the student, the message and two mapping tables the staff_message and stu_message. These tables contain only the student/staff id and the message id.
My problem is that I cannot order the messages. I mean that I cannot figure out how can I make one SQL statement that will return all messages and be ordered by for example the ID. The code that I have made is this:
$qu = mysqli_query($con,"SELECT * FROM stu_message");
while($row7 = mysqli_fetch_assoc($qu)){
$que = mysqli_query($con, "SELECT * FROM student WHERE studentid =".$row7['stu_id']);
while($row8 = mysqli_fetch_assoc($que)) {
$username = $row8['username'];
}
$query3 = mysqli_query($con, "SELECT * FROM message WHERE id=".$row7['mid']);
while($row6 = mysqli_fetch_assoc($query3)) {
echo $row6['date']."<strong> ".$username."</strong> ".$row6['text']."<br>";
}
}
$query2 = mysqli_query($con, "SELECT * FROM staff_message");
while($row3 = mysqli_fetch_assoc($query2)){
$query = mysqli_query($con, "SELECT * FROM staff WHERE id =".$row3['staff_id']);
while($row5 = mysqli_fetch_assoc($query)) {
$username = $row5['username'];
}
$query3 = mysqli_query($con, "SELECT * FROM message WHERE id=".$row3['m_id']);
while($row6 = mysqli_fetch_assoc($query3)) {
echo $row6['date']."<strong> ".$username."</strong> ".$row6['text']."<br>";
}
}
?>
The result is different from that I want. To be more specific first are shown the messages from the students and then from the staff. My question is, is there any query that it can combine basically all these four tables in one and all messages will be shown in correct order? for example by the id?
Thank you in advance!
First, use JOIN to get the username corresponding to the stu_id or staff_id, and the text of the message, rather than separate queries.
Then use UNION to combine both queries into a single query, which you can then order with ORDER BY.
SELECT u.id, u.text, u.username
FROM (
SELECT s.username, m.text, m.id
FROM message AS m
JOIN stu_message AS sm ON m.id = sm.mid
JOIN student AS s ON s.id = sm.stu_id
UNION ALL
SELECT s.username, m.text, m.id
FROM message AS m
JOIN staff_message AS sm ON m.id = sm.m_id
JOIN staff AS s ON s.id = sm.staff_id
) AS u
ORDER BY u.id

PHP Query MySQL Tables from IDs from other tables

I have a database with several tables. I'm able to query IDs from a single table. What I'd like to do is Use those IDs to query another tables IDs, then use these new IDs to query fields from the final table. Here is what I am currently doing:
Here is how I acquire the first set of IDs:
$returnedPost = mysqli_query($con, "SELECT Region_ID FROM Region WHERE RegionName='" . $queryVar . "'");
function resultToArray($result) {
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
$rows = resultToArray($returnedPost);
//$rows[x]['Region_ID'];//returns Region_ID 1...n
I'd like to use the IDs in $rows to be able to query a new set of IDs from other tables as follows:
$newTbl = mysqli_query($con, "SELECT Location_ID FROM Location WHERE Region_ID=" . $rows[$x]['Region_ID']);
$rows2 = resultToArray($newTbl);
$finalTbl = mysqli_query($con, "SELECT Field1, Field2 FROM Posts WHERE Location_ID=" . $rows2[$x]['Location_ID']);
Can someone please tell me how I can accomplish this? Thanks.
you can use INNER JOIN in one query to get at this data, maybe something like this
SELECT P.Field1,P.Field2
FROM Region R
INNER JOIN Location L ON R.Region_ID = L.Region_ID
INNER JOIN Posts P ON L.Location_ID = P.Location_ID
WHERE R.RegionName = Your_Region_QueryVar

Select from mysql where it does not equal row in other table

I am doing a foreach loop and selecting each row that equals that section name. However, I need to also show all results that don't equal any of the section names. Below is the part of my code that is selecting rows where it equals the section name.
$result2 = mysqli_query($con, "SELECT * FROM sections ORDER BY `order`");
$sectionnames = array();
while($row = mysqli_fetch_array($result2)) {
$sectionnames[] = $row['sectionname'];
}
foreach ($sectionnames as $sectionname) {
$result = mysqli_query($con,"SELECT * FROM faq WHERE section = '$sectionname' ORDER BY `order`");
Do you mean something like this:
SELECT * FROM `faq` WHERE `section` NOT IN (SELECT `sectionname` FROM `sections`)
which would select all faq items that do not have a section that shares a name with any sectionname in the sections table? If not then perhaps what juergen d posted is what you need. It's a little unclear exactly what your end goal is, so feel free to clarify and I will update.
I think it should be,
$result = mysqli_query ($con, "SELECT * FROM faq WHERE section NOT IN
(SELECT sectionname FROM sections)");
You can use a left join to get the data in 1 query
SELECT *
FROM sections s
left join faq f on f.section = s.sectionname
ORDER BY s.`order`, f.`order`
SELECT section FROM Faq
LEFT JOIN Sections
ON Faq.section = Sections.sectionname
WHERE Sections.sectionname IS NULL

How to get information from 2 tables at once in PHP and MySQL?

<?php
include('includes/config.php');
$topi = $_GET['id']; //id of url
mysql_select_db("ban", $con);
$query = "SELECT * FROM `basic` WHERE id = '$topi' LIMIT 0, 30";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$aa = $row['item'];
$cc = $row['moreinfo'];
$dd = $row['contactinfo'];
$ff = $row['id'];
In this script, I get information from the table basic, but I want to retrieve data from another table named users. How can I retrieve data from two tables at once?
users table consists of following columns:
email
username
ID
You need to JOIN the two tables on a common value, called a foreign key. Once you've posted the structure of the users table as requested in the comments, I can provide a more complete example.
EDIT: See example. This calls explicit column names instead of SELECT *.
$query = "SELECT
basic.id,
basic.item,
basic.moreinfo,
basic.contactinfo,
users.email,
users.username
FROM basic JOIN users ON basic.id = users.id
WHERE id = '$topi'
LIMIT 0 , 30";
You would use a JOIN onto the other table.
$query = "SELECT *
FROM basic b
JOIN users u ON b.user_id = u.user_id
WHERE id = '$topi'
LIMIT 0, 30";
Something like that, but based on your fields.
Please Note: the ON clause specifies what you will be looking for a match on.

Categories