i have 3 tables i want to show the data from them to a page.

table1 "trip"
table2 "seat"
table3 "user_information"

show all the data of table one which is working its showing but how can i show multiple table data any ways.

show all data of trip and seat

from user_information it shows

first_name
last_name

all the tables have id with the same name.

here is the code im using.

<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("online_bus_project", $con);

$result = mysql_query("SELECT * FROM trip, seat, user_information ORDER BY `id` DESC LIMIT 1");  


echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>From</th>
<th>To</th>
<th>Date</th>
<th>Fare</th>
<th>Seat</th>

</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['first_name'] . "</td>";
  echo "<td>" . $row['last_name'] . "</td>";
  echo "<td>" . $row['from'] . "</td>";
  echo "<td>" . $row['to'] . "</td>";
  echo "<td>" . $row['date'] . "</td>";
  echo "<td>" . $row['fare'] . "</td>";
  echo "<td>" . $row['seat'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Dani AI

Generated

Short summary and what most people miss: and were right to point you at joins, but a join that returns no rows usually means the columns you’re joining on aren’t the actual relationships (or you’ve got name collisions / reserved-word columns). Before changing code, verify the schema: which column is the trip primary key, which column (if any) on seat points to trip, and which column links a seat to a user. Many beginners assume every table’s id is the same thing — often it isn’t.

Quick checklist to find the problem

  • Confirm column names/keys with DESCRIBE or SHOW CREATE TABLE for each table and note the actual foreign-key columns.
  • Run a simple single-table SELECT from each table to inspect sample id values — if values don’t match, your join will be empty.
  • Avoid SELECT * when joining tables that share column names (like id) — pick and alias columns so PHP array keys don’t get overwritten.
  • If columns are named from or to, quote them or alias them because they clash with SQL keywords.

Example (adjust to your actual foreign-key names)

SELECT
  u.first_name, u.last_name, u.contact_no,
  t.`from` AS trip_from, t.`to` AS trip_to, t.date, t.fare,
  s.seat
FROM trip AS t
LEFT JOIN seat AS s ON s.trip_id = t.id
LEFT JOIN user_information AS u ON u.id = s.user_id
ORDER BY t.id DESC
LIMIT 1;

Use LEFT JOIN while debugging so you still see trip rows even if a seat or user is missing. Fetch associative rows and var_dump() one row to see exactly which keys exist — that will show whether contact_no is present or being overwritten.

A few practical notes: select explicit columns and give them unique aliases, check mysql_num_rows()/rowCount, and move off the old mysql_* extension to mysqli or PDO for better error handling and prepared statements.

Recommended Answers

All 14 Replies

any one?

Look into mysql joins or using the where clause and table aliases.

Ex.

SELECT * FROM trip t,seat s,user_information u WHERE t.id = s.id AND t.id = u.id

Look into mysql joins or using the where clause and table aliases.

Ex.

SELECT * FROM trip t,seat s,user_information u WHERE t.id = s.id AND t.id = u.id

that's not working??? its not showing any error but its also not showing the result all the tables are blank

check this?

$result = mysql_query("SELECT * FROM trip t,seat s,user_information u WHERE t.id = s.id AND t.id = u.id");  





echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>From</th>
<th>To</th>
<th>Date</th>
<th>Fare</th>
<th>Seat</th>

</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['first_name'] . "</td>";
  echo "<td>" . $row['last_name'] . "</td>";
  echo "<td>" . $row['from'] . "</td>";
  echo "<td>" . $row['to'] . "</td>";
  echo "<td>" . $row['date'] . "</td>";
  echo "<td>" . $row['fare'] . "</td>";
  echo "<td>" . $row['seat'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

Do you have a column named id in your table. That was just an example.

Have you got WHERE twice? It is on my screen twice.
Dani.

yes id is in every table? any solutions to this?

Have you got WHERE twice? It is on my screen twice.
Dani.

that's one time after that is 'AND'

can any one show me how to display that correctly? with one table its working fi9 any ideas.

Table (trip) show all from this
* id
* from
* to
* date
* fare

Table (seat) show all from this also
* id
* seat

Table (user_information) show only first_name and last_name
* first_name
* last_name
* email
* address
* city
* province
* contact_no

user_information has no id.

yes it has id i did mistake on writing its named same as id kkeith29?

any solution?

It should work then. Something else has to be wrong.

add this: or die(mysql_error()); to the end of the mysql_query function. That way you can see if there are any database problems.

commented: thanx for the help +1

i have done it like this but its not showing contact number why????

because its a number that's why?

try this..

select  t.id,t.from,t.to,t.date,t.fare,s.seat,u.first_name,u.last_name from trip t,seat s,user_information u where t.id=s.id
commented: thanx for the help +1

thanx for the help all really appreciate.

thanx
Poojasrivastava rep added also for kkeith29 thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.