Craig
Craig

Reputation: 783

how to make multiple queries in PHP

im very new to this and have a very basic knowledge php and SQL this is my first website, please could some one help.

I currently have information being pulled from my database to my website, which is good.

however i want to run another couple of queries on the same html page one that will display a list of the latest 5 entries in the database and sort them by 'location_id' descending, the other will display a list of 5 entries and display them random every time the page refreshes. see my code below:

?php
    require "connect.php";
    $query =  "select * from location";
    $result = @mysql_query($query, $connection) 
    or die ("Unable to perform query<br>$query");
?>

<?php
while($row= mysql_fetch_array($result))
{
?>

<?php echo $row['location_id'] ?>

?php } ?> 

Also when i was experimenting trying to solve this myself, i duplicated the loop shown above in a different location, and it would not display anything, can sone one explain why?

Thanks Guys

Upvotes: 2

Views: 1036

Answers (3)

MyHeadHurts
MyHeadHurts

Reputation: 1562

You could reset the query to do this or

create a new query and result with different names and then when the page runs have it select one of the queries to run.

$query1 =  "select * from location";
$result1 = @mysql_query($query1, $connection); 
or die ("Unable to perform query<br>$query1");

resetting might be easier but this is an option too

Upvotes: 0

mmdel
mmdel

Reputation: 1279

i have just seen the syntax.

i guess ";" is missing in

should be

<?php echo $row['location_id'] ; ?>

Upvotes: 0

Cem Kalyoncu
Cem Kalyoncu

Reputation: 14593

It needs reset,

 mysql_data_seek( $result, 0 );

Upvotes: 5

Related Questions