来自查询PHP的第一个数组

时间:2014-03-25 12:54:09

标签: php sql

我的代码有问题。我想从查询中获取第一个数组并在显示下一个2之后显示它。

对于查询中的第一个数组,我想为接下来的2只显示img标题

我的代码:

$link = mysql_connect('server', 'user', 'password')
        or die("Can't");
$db = mysql_select_db('database')
      or die ("can't");
$sql = 'SELECT * FROM News LIMIT 3';
$retval = mysql_query($sql,$link);
$filename ="footer.html";
$handle = fopen("footer.html", 'w+');

while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    //echo "<pre>"; print_r($row); echo "</pre>";
    $content = '<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
        <div class="block_articles">
        <div class="block_header" >
        <div class="headerlink" >
        <a href="'.$sitelink.'" target="_blank"></div></div>';
    $content .= '<img src="'.$sitelink.'news/'.$row['ImageName'].'" alt="'.htmlspecialchars($row['Title']).'" >';   
    $content .= '<a href="="">'.$row['Title'].'</a>';
    $content .= '<div class="date">';
    $content .= $row['Date'];
    $content .= '</div>';
    $content .= '</div>';
    echo $row['Title'].'<br>';
    if ($handle) 
    {
        if (!fwrite($handle, $content))
        die("can't");
    }
}

1 个答案:

答案 0 :(得分:0)

根据您的评论,您希望使用基本循环计数器并根据计数器更改行为:

theCurrentRow = 0;
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    theCurrentRow++;
    if (theCurrentRow == 1) {
    //show the images
    }
    else {
    // show the title
    }

}

同样重申@ AmalMurali的评论:不要使用mysql_ *函数,它们已被弃用:http://php.net/manual/en/mysqlinfo.api.choosing.php