Select Query Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PearlExportBen
Forum Newbie
Posts: 1
Joined: Wed Nov 09, 2011 6:04 am

Select Query Help

Post by PearlExportBen »

Hi All, I have the following Query:

Code: Select all

$query = 'SELECT `BookingID`,`OfferID`, `name`, `OfferCount`,`OfferPrice` FROM `Offers_Finance` JOIN offers_details ON `OfferID` =  `o_d_id` WHERE `BookingID` ="' .$bookerid. '"';
	if ( !$result = $mysqli->query( $query ) ) { die( $mysqli->error ); } 
	$field = $result->fetch_object();

This will return anywhere between 0 and 50 Rows of Data. For Each Row returned, I want to do the following:

Code: Select all

       echo '<tr>';
       echo '<td>' . $field->name.'</td>';
       echo '<td></td>';
       echo '<td>&pound;' . $field->OfferPrice.'</td>';
       echo '<td></td>';
       echo '<td>' . $field->OfferCount . '</td>';
       echo '<td></td>';
       echo '<td>&pound;' . $field->OfferPrice * $field->OfferCount . '</td>';
       echo '</td>';
       echo '</tr>';

The problem I have is - how do I loop through the results returned from the query to output multiple rows?

Sorry if this doesn't make much snese, I'm new to PHP and the whole web development world, but have been landed with finishing a project someone else started!

If someone would be kind enough to turn this into a working example, it would help no end as I have about 20 of these things to figure out across the project!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Select Query Help

Post by social_experiment »

Looping through things can be accomplished by using while, for loops.

Code: Select all

<?php
 $query = mysql_query($sql);
 
 while ($obj = mysql_fetch_object($query)) {
    echo $obj->field1;
    echo $obj->field2;
 }
?>
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Select Query Help

Post by mikeashfield »

Unless there are no more than 50 records in the offers_finance table then I don't see where you're limiting the results to 50 per query.
Post Reply