Multiple Table Search and show the result - php

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'";

Related

Selecting multiple tables 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;

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

select from two mysql tables where column value is similar and id is retrieved from previous page

i have a question. my english isn't well. so i hope i explain well...
i have two tables, tbl_home and tbl_office, the question is
how do i make a select statement from 2 tables which have identical value from column 'case_no' where it is referenced in both table..
$a=$_POST['home_id']
the code above is where i get the home_id from,
while the statement below is how i try to select both tables based on value in column 'case_no' of both table. but it is based on variable $a which i retrieved from form
<?php
$sql2 = "SELECT * FROM tbl_office WHERE case_no IN (SELECT * FROM tbl_home WHERE home_id = '$
$result2=$conn->query($sql2);
while($row = $result2->fetch_assoc()){
$a=$row['case_no'];
$bc=$row['colour'];
echo " $a <br/> ";
echo " $bc2 <br/>";
?>
is the select statement above correct??
soo, i just want anybody to take a look a this specific statement and how to make it right
$sql2 = "SELECT * FROM tbl_office WHERE case_no IN (SELECT * FROM tbl_home WHERE home_id = '$a'";
You need inner join to use:
" SELECT t_office.home_id,t_office.case_no,t_office.name FROM tbl_office
t_office INNER JOIN tbl_home t_home ON t_office.case_no = t_home.case_no;
where t_office.case_no ='$a'";
u can use "inner join" for example:
"SELECT t.home_id,t.case_no,t.name FROM tbl_office
t INNER JOIN tbl_home h ON h.case_no = h.case_no"
**select tbl_home.name,tbl_office.case_no,tbl_office.color from tbl_office
INNER JOIN tbl_home on tbl_office.case_no = tbl_home.case_no
where tbl_office.case_no ='$a';**
I hope this will be working fine until $a(case_no) value is existed in tbl_home or else it doesn't give any rows

How to join two tables with MySQL and display data using PHP

I am trying to join two tables and assign a task from the "tasks" table to a user in the "users" table.
My tables and columns are:
tasks:
task_id,
task_description,
userid
users:
user_id,
first_name,
last_name
<?php
$join_query = "
SELECT task_id
, task_description
, tasks.userid
, users.user_id
, first_name
, last_name
FROM tasks
JOIN users
ON tasks.userid = users.user_id
";
$result = mysqli_query($connection, $join_query);
while($row = mysqli_fetch_array($result)) {
$name = $row['first_name'];
$description = $row['task_description'];
}
?>
The table join seems to be working as I can echo the data with different aliases. But I'm trying to make it possible to assign tasks to users. Am I even close? Any help would be appreciated. Thanks!
First look at your query and change it to this.
$join_query = "
SELECT tasks.*
, users.*
FROM tasks
INNER JOIN users
ON tasks.userid = users.user_id
";
Then fetch an associative array of the data:
while($row = mysqli_fetch_assoc($result)) {
$name = $row['first_name'];
$description = $row['task_description'];
}
As you were selecting all fields of both table so * will do this for you. And when making a join it is good approach to use the table name with field name. And in the fetch an associative array to get results. Everything else is fine :)

Select data from 2 MySQL tables - PHP

I have two tables:
Table 1
contacts
id
email
name
lastname
postcode
straat
huisnr
woonplaats
klantnummer
bsn
land
debiteurnummer
Table 2
contacts_group
id
mail
group_name
How can I select and order this 2 tables in one query. I've tried union and left join, but did not work.
$result = mysqli_query($database->connection,
"SELECT *
FROM contacts
WHERE owner = '$session->username'
ORDER BY name ASC ,bedrijfsnaam ASC")
or die(mysqli_error());
while($roww = mysqli_fetch_array($result)){
echo $roww['email'];
echo $roww['name'];
}
Table contacts_group:
$result = mysqli_query($database->connection,
"SELECT *
FROM contacts_group
WHERE owner = '$session->username'
ORDER BY group_name ASC")
or die(mysqli_error());
while($roww = mysqli_fetch_array($result)){
echo $roww['mail'];
echo $roww['group_name'];
}
Escape your variables. You should not put the php variable unescaped in sql query. You can suffer from sql injections.
If you want to join two tables by foregin key you can do:
SELECT * FROM contacts, JOIN contacts_group ON contacts_group.id = contacts.group_id
WHERE contacts.owner = '$session->username' ORDER BY contacts.name
But you are missing group_id in contacts table or some foregin key to connect two tables.
Before do it read this topic, after try to use mysql join construction

Categories