Web Hosting Talk







View Full Version : PHP/PEAR/MYSQL Loop Results?


pwalters09
02-09-2010, 11:48 PM
Hi guys,

Basically I am a bit of a noob when it comes to PEAR, and am wondering if anyone can help...

Basically what I need to do is query my database, then loop the results into a select box...

I need to select 2 fields from a database that match query, then loop the results (like mysql_fetch_assoc() but with pear)

Here is my code:


<h2>{$advertise}</h2>
<table border="0" id="contactform">
<form action="{php} echo $_SERVER['PHP_SELF'] {/php}" method="post">
<tr><td>{$websiteurl} <span class="red">*</span></td>
<td><input id="website" name="website" class="text" /></td></tr>
<tr><td>{$description} <span class="red">*</span></td>
<td><input id="desc" name="desc" class="text" /></td></tr>
<tr><td>{$advertisement} {$package} <span class="red">*</span></td>
<td><select name="package">

{php}
$qry = "SELECT amount, price FROM packages WHERE type = 'ads'";
$query = $db_object->query($qry);

if (DB::isError($query)) {
die($query->getMessage());
}

while ($query->fetchInto($row, DB_FETCHMODE_ASSOC)) {
extract($row);
print "<option value='$amount'>$amount visits for $$price</option>\n";
}

{/php}

</select></td></tr>
<tr><td>{$name} <span class="red">*</span></td>
<td><input id="name" name="name" class="text" /></td></tr>
<tr><td>{$pemail} <span class="red">*</span></td>
<td><input id="pemail" name="pemail" class="text" /></td></tr>
<tr><td><span class="red"></span></td>
<td align="right"><input type="submit" name="submit" value="{$submit}"></td></tr>
</form>
</table>


Can anyone help?

Thanks

Driver01
02-10-2010, 08:25 AM
Is this not working for you at the moment? Sorry for the obvious question but what exactly is the problem at the moment?

pwalters09
02-10-2010, 09:10 AM
No for some reason I keep getting a 'call to a member function in a non-member object' errors when trying to do it.

Driver01
02-10-2010, 09:51 AM
are all the required files included?

pwalters09
02-10-2010, 10:22 AM
yes everything required (db connection script etc) included in the main php script.

Driver01
02-10-2010, 11:33 AM
$query = $this->db->query($qry);

pwalters09
02-10-2010, 12:03 PM
that just fetches 1 result doesnt it? i need to loop the results

Driver01
02-10-2010, 12:11 PM
No the while loop on the query and the fetchInto function should return as many results as you have, I am just trying to find out what the non-object is reffering too, I am presuming its $db_object. What is the whole error?