I have two tables (client_login,co_passenger).i want to display fname from both tables in select box.i have tryed this.but it display fname from only one table.
<select name="client" class="category_list" id="dynamic_select" >
<?php
$query= "SELECT c.fname,p.fname FROM client_login c,co_passenger p" ;
$result= mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
echo '<option value="directory.php?cat_id='.$row['fname'].'">'.$row['fname'].'</option>';
}
?>
</select>
i want to display aa,bb,cc,dd in select box
client_login
co_passenger
You have to use two queries merged together using union
$query= "SELECT fname FROM client_login
union
select fname from co_passenger "
This will return combined results.
Related
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'";
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;
I'm trying to create a list of members in an HTML option list, but i want it to be divided according to the department of each member, because every member is working in aone department of the company .
Here is the code i tried to use but was unsuccessful :
<?php
echo'<select size=1 name="Name List">'."\n";
echo'<option value="-1">--Name List--</option>'."\n";
$result1=mysql_query("select department from users");
while($data1=mysql_fetch_array($result1)){
echo'<optgroup value="'.$data1[0].'">'.$data1['department'];
$result=mysql_query('select username from users where department="'.$data1['department'].'" ');
while($data=mysql_fetch_array($result)){
echo'<option value="'.$data[0].'">'.$data['username'];
echo'</option>'."\n";
}
echo'</optgroup>'."\n";
}
echo'</select>'."\n";
mysql_close();
?>
first of all its more better to have separate tables one for members and another for departments.
every tables have their own id and other fields.in this case you need to bring department_id in members table as foreign key.
departments table:
members table:
so next in your code you should join these two tables to get related data's:
code:
<?php
$con = mysql_connect("localhost","username","pass");
mysql_select_db("test");
$sql = "select members.name as mem_name,departments.name as dep_name
from members
left join department
on
members.department_id = departments.id
order by members.name asc";
$result = mysql_query($sql,$con);
while ($row= mysql_fetch_array($result))
{
echo '<optgroup label="'.$row['dep_name'].'">\n';
echo '<option value="'.$row['mem_name'].'">'.$row['mem_name'].'</option>\n';
echo '</optgroup>\n';
}
?>
Result:
I am trying to get data of 'indication' from two different tables.
The script works fine when selecting FROM number_one only.
Did try this with a , inbetween but that doesn't work.
How should I do this?
query2 = mysql_query("SELECT `indication` FROM `number_one`, `number_two` ORDER BY `indication` DESC");
while($row2 = mysql_fetch_object($query2)){
if($explode2[1] == $row2->indication){
echo "<option value=\"$row2->indication\" selected=\"selected\">$row2->indication</option>";
}
else{
echo "<option value=\"$row2->indication\">$row2->indication</option>";
}
}
Solution
query2 = mysql_query("SELECT `indication` FROM `number_one` UNION ALL SELECT `indication` FROM `number_two` ORDER BY `indication` DESC");
while($row2 = mysql_fetch_object($query2)){
if($explode2[1] == $row2->indication){
echo "<option value=\"$row2->indication\" selected=\"selected\">$row2->indication</option>";
}
else{
echo "<option value=\"$row2->indication\">$row2->indication</option>";
}
}
Assuming you have two tables, number_one and number_two with the indication column, your query would cross-join the two tables, giving you a set of rows which each have two columns (number_one.indication and number_two.indication, in all the possible combinations.
If I understand the question correctly, you want to have them as one column, with all the values from both tables. This can be done using the UNION ALL set operator, as I demonstrate below:
SELECT indication
FROM ( SELECT indication
FROM number_one
UNION ALL
SELECT indication
FROM number_two)
ORDER BY 1 DESC
I have a script designed to print out values of students who have accrued more than 3 tardies. I want this script to print out both the student name, and the amount of times they've been tardy, but so far I've been only able to print out their names with the following script:
$sql = "select DISTINCT StudentID,classid,date from attendance_main where status = 'Tardy' AND classid like '%ADV%'";
$result = mysql_query($sql) or die (mysql_error());
while($row=mysql_fetch_array($result)) {
$studentid = $row['StudentID'];
$sql2 = "select distinct StudentID,classid,date from attendance_main where StudentID = '$studentid' AND status = 'Tardy' AND classid like '%ADV%'";
$result2 = mysql_query($sql2) or die (mysql_error());
while($row2=mysql_fetch_array($result2)) {
$tardycount = mysql_num_rows($result2);
$studentid = $row2['StudentID'];
if($tardycount >= 3) {
$sql3 = "select * from students where rfid = '$studentid'";
$result3 = mysql_query($sql3) or die (mysql_error());
while($row3=mysql_fetch_array($result3)) {
$fname[] = $row3['fname'];
}
}
}
}
$newdata = array_unique($fname);
foreach ($newdata as $value) {
echo $value;
}
I can't think of how to intuitively do this. Keeping it all in the while loop didn't work (I had multiple results coming up for the same students despite requesting unique entries) so using array_unique was the only method I could think of.
Thanks for any help!
Something like this:
SELECT
attendance_main.StudentID,
students.fname,
COUNT(attendance_main.*) AS `times_tardy`
FROM
attendance_main
INNER JOIN
students
ON
attendance_main.StudentID = students.rfid
WHERE
attendance_main.status = 'Tardy'
AND
attendance_main.classid like '%ADV%'
GROUP BY
attendance_main.StudentID
HAVING
`times_tardy` > 3
Joining the two tables gets you the tardy count and student's name in one query, and the GROUP BY and HAVING clause that get you only the students with more than 3 tardy entries.
You can (and should) do almost everything in SQL. It should look something like this.
select StudentID, classid, date count(*)
from attendance_main
where status = 'Tardy' AND classid like '%ADV%'"
left join student on student.rfid = attendance_main.StudentId
group by StudentId
having count(*) > 3;
Here's how it works.
select the results you want to work with:
select StudentID, classid, date count(*)
from attendance_main
where status = 'Tardy' AND classid like '%ADV%'"
Join the students to your result set on the common id
left join student on student.rfid = attendance_main.StudentId
group everything by student. We use count(*) to get the number of items. We know we're only dealing with tardies because of the where clause in the select.
group by StudentId
limit the results to only tardies above 3 with the having claues (this is like a where clause for the group by)
having count(*) > 3;