Mysql select and use in php
I am trying to figure this out, I want to call several rows say in the range of 5 - 40 from mysql. I then would like to take the four variables in each row and use them in php in a foreach statement.
I was hoping someone might be bale to give me a hand. This is the code I am working with right now.
include ('php/opendb.php');
$frontad_mysql = mysql_query("SELECT title, city, ad_image, rent FROM rentals WHERE front_page_ad = '1' AND paid = '1' ") or die(mysql_error());
$front_ads = array();
while($front_ads = mysql_fetch_assoc($frontad_mysql)) {
$front_ads[] = $row;
}
$ads = "<div id='ad_container'>";
$x = 0;
foreach ($front_ad as $fa){
$ads .= "<div id='$x' class='front_ads'>".$fa['$x']['title']."&l开发者_如何学JAVAt;br/><img src='".$fa['$x']['ad_image']."' class='frontad_image' /><br/>".$fa['$x']['city']."<br/>Price: $".$fa['$x']['rent']."</div>";
}
$ads .= "</div>";
mysql_close();
Any help is greatly appreciated - Thanks
You're using
$fa['$x']
Single quotes doesn't replace expand variables .. it takes everything literally. Change it into
$fa["$x"]
or
$fa[$x]
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
Shouldn't this:
while($front_ads = mysql_fetch_assoc($frontad_mysql)) {
$front_ads[] = $row;
}
Be more like this:
while($row = mysql_fetch_assoc($frontad_mysql)) {
$front_ads[] = $row;
}
The first one is just pulling some data out of the database, trying to append $row
onto it, and then throwing it all away. That's probably not what you want to do.
you can achieve first your objective of getting only ranged record can be completed by LIMIT ..like this
SELECT title, city, ad_image, rent FROM rentals WHERE front_page_ad = '1' AND paid = '1' LIMIT 5,35
it will only bring 5-40 data ..
精彩评论