用php回显mysql查询结果

发布于 12-07 16:10 字数 939 浏览 1 评论 0原文

我有这个查询,

$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
$row_people = mysql_fetch_assoc($people);
$totalRows_people = mysql_num_rows($people);

我可以使用像这样的 while 循环来回显无序列表中的结果

<ul>

<?php {do { ?>

    <li><?php echo $row_people['name'];?></li> 

<?php } while ($row_people = mysql_fetch_assoc($people));}?>

</ul>

,但我不能使用它,因为我的 html 不允许它。

<ul>
    <li class="first">
        <a href="?name=kate">Kate</a>
    <li>
    <li class="second">
        <a href="?name=john"><img src="john.jpg" />John</a>
    <li>
    <li class="third">
        <a href="?name=max"><span>Max</span></a>
    <li>
</ul>

我的问题是如何将从数据库检索到的名称回显到该 html 中的适当位置?

感谢您的帮助。

I have this query

$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
$row_people = mysql_fetch_assoc($people);
$totalRows_people = mysql_num_rows($people);

I can echo the results within a unordered list using a while loop like this

<ul>

<?php {do { ?>

    <li><?php echo $row_people['name'];?></li> 

<?php } while ($row_people = mysql_fetch_assoc($people));}?>

</ul>

But I can't used this as my html does not allow it.

<ul>
    <li class="first">
        <a href="?name=kate">Kate</a>
    <li>
    <li class="second">
        <a href="?name=john"><img src="john.jpg" />John</a>
    <li>
    <li class="third">
        <a href="?name=max"><span>Max</span></a>
    <li>
</ul>

My question is how can echo the name that was retrieved from the database into the appropriate place within this html?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栩栩如生2024-12-14 16:10:59

试试这个:

<?php

$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());

if(mysql_num_rows($people) > 0){
?>
    <ul>
    <?php
        while ($row_people = mysql_fetch_assoc($people)){ 
    ?>
            <li><?php echo htmlentities($row_people['name']);?></li> 
    <?php
        }      
    ?>
    </ul>

<?php
}

?>

Try this:

<?php

$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());

if(mysql_num_rows($people) > 0){
?>
    <ul>
    <?php
        while ($row_people = mysql_fetch_assoc($people)){ 
    ?>
            <li><?php echo htmlentities($row_people['name']);?></li> 
    <?php
        }      
    ?>
    </ul>

<?php
}

?>
乖乖2024-12-14 16:10:59

您只需为每个“类型”的用户创建一个渲染器(假设您在用户行上有一个类型属性)或根据其属性创建一个渲染器。例如,假设您必须根据属性进行过滤:

<?php

function render_simple($person) {
    return '<a href="?' . $person['name'] . '">' . $person['name'] . '</a>';
}

function render_with_image($person) {
    return '<a href="?' . $person['name'] . '"><img src="' . $person['image'] . '.jpg"/>' . $person['name'] . '</a>';
}

function render_special($person) {
    return '<a href="?' . $person['name'] . '"><span>' . $person['name'] . '</span></a>';
}

function render_person($person) {
    if ($person['image']) {
        return render_with_image($person);
    }
    if ($person['special']) {
        return render_special($person);
    }
    return render_simple($person);
}

$i = 0;
while ($row_people = mysql_fetch_assoc($people)){ ?>
    <li class="index<?php echo ++$i; ?>">
        <?php echo render_person($person); ?>
    </li>
<?php
}      
?>

这应该可以工作,但您现在将拥有index1、index2 等,而不是类名first、second 等。

You'll just have to create a renderer for each "type" of user (assuming you have a type property on the user rows) or based on their attributes. For example, let's say you're going to have to filter based on the attributes:

<?php

function render_simple($person) {
    return '<a href="?' . $person['name'] . '">' . $person['name'] . '</a>';
}

function render_with_image($person) {
    return '<a href="?' . $person['name'] . '"><img src="' . $person['image'] . '.jpg"/>' . $person['name'] . '</a>';
}

function render_special($person) {
    return '<a href="?' . $person['name'] . '"><span>' . $person['name'] . '</span></a>';
}

function render_person($person) {
    if ($person['image']) {
        return render_with_image($person);
    }
    if ($person['special']) {
        return render_special($person);
    }
    return render_simple($person);
}

$i = 0;
while ($row_people = mysql_fetch_assoc($people)){ ?>
    <li class="index<?php echo ++$i; ?>">
        <?php echo render_person($person); ?>
    </li>
<?php
}      
?>

This should work, with the exception that instead of class names first, second, etc, you'll now have index1, index2, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文