doson1998 2011-08-04 03:42
浏览 15
已采纳

Mysql选择并在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']."<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

  • 写回答

3条回答 默认 最新

  • douyue7536 2011-08-04 03:52
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘