0

I am trying to dsiplay data from 2 different tables from my mysql database using a while loop.

Currently I can display the data from 1 table and limit the results to 3. I then want to display the first 5 records from another table. If I join the tables I can only display the same number of items from both using LIMIT?

I am using a while loop to display the content from a table called item, using the following code;

$query"); $result2 = @mysql_query($query, $connection) or die ("Unable to perform query
$query");

<?php
while($row= mysql_fetch_array($result))
{
?>
<?php echo $row['item'] ?>
<?php
}
?>

If I start another loop for the data from the next table called movie, however the data is not displayed using the following code;

<?php
while($row= mysql_fetch_array($result2))
{
?>
<?php echo $row['title'] ?>
<?php
}
?>

What is the best way to display the data from the 2 tables? Many Thanks

1
  • If you join you can limit to 8 which would be 5+3. But yes, there is only one LIMIT clause per SELECT query. Commented Jun 21, 2011 at 17:17

1 Answer 1

3

I don't know if you forgot to paste a bit of code, but this should work:

<?php
$query =  "select * from item order by date, time asc limit 0,3";
$result = mysql_query($query);
while($row= mysql_fetch_array($result))
{
   echo $row['item'];
}

$query2 = "select * from movie limit 0,5";
$result2 = mysql_query($query2);    
while($row= mysql_fetch_array($result2))
{
   echo $row['movie'];
}
?>

You may be able to do it with one SQL Query too:

SELECT i.item, m.movie
FROM (SELECT * FROM item ORDER BY date, time ASC LIMIT 0,3) i,
     (SELECT * FROM movie limit 0,5) m

Then in php:

<?php
while($row= mysql_fetch_array($result))
{
   echo $row['item'];
   echo $row['movie'];
}
?>

But that depends on how you want to format the output.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.