mysql 语句显示表或消息

发布于 2024-12-14 17:23:06 字数 824 浏览 4 评论 0原文

我的数据库中有一个订单表,一旦用户登录他的帐户,他/她就可以查看他/她以前的订单。

我有以下代码:

    <?php

$result = mysql_query("SELECT * FROM `order` WHERE username = '". $_SESSION['username']."' ")
or die(mysql_error()); ;

echo "<table border='0'><table border width=100%><tr><th>Product</th><th>Quantity</th><th>Price</th><th>Date</th>";
while($info = mysql_fetch_array($result))
{
        echo "<tr>";
        echo "<td>" . $info['name']. "</td>";
        echo "<td>" . $info['quantity']. "</td>";
        echo "<td>" . $info['price']. "</td>";
        echo "<td>" . $info['date']. "</td>";

}
echo "</tr>";
echo "</table>";
?>

我使用什么 mysql 语句来显示消息“您还没有订购任何东西”而不是空白表?

谢谢

I have an order table in my database and once a user logs into his account, he/she can view his/her previous orders.

I have the following code for it:

    <?php

$result = mysql_query("SELECT * FROM `order` WHERE username = '". $_SESSION['username']."' ")
or die(mysql_error()); ;

echo "<table border='0'><table border width=100%><tr><th>Product</th><th>Quantity</th><th>Price</th><th>Date</th>";
while($info = mysql_fetch_array($result))
{
        echo "<tr>";
        echo "<td>" . $info['name']. "</td>";
        echo "<td>" . $info['quantity']. "</td>";
        echo "<td>" . $info['price']. "</td>";
        echo "<td>" . $info['date']. "</td>";

}
echo "</tr>";
echo "</table>";
?>

what mysql statement do i use to show a message 'you have not ordered anything yet' instead of a blank table?

thanks

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

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

发布评论

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

评论(3

云裳 2024-12-21 17:23:11

您可以使用 mysql_num_rows() 计算语句中的结果数

if (mysql_num_rows($result) == 0) {
  echo '<p>You have not ordered anything yet.</p>';
}
else {
  echo "<table...";
}

此外,您可能希望在 while 循环内移动倒数第二行 (echo "";)这样 HTML 就可以正确输出了。

You can count the number of results in your statement using mysql_num_rows():

if (mysql_num_rows($result) == 0) {
  echo '<p>You have not ordered anything yet.</p>';
}
else {
  echo "<table...";
}

Also, you probably want to move the second to last line (echo "</tr>";) inside your while loop so the HTML is outputted correctly.

疯了 2024-12-21 17:23:10

您可以使用 mysql_num_rows()获取结果集中的行数。

$rows = mysql_num_rows($result);
if($rows == 0) {
   echo 'you have not ordered anything yet';
}

You could use mysql_num_rows() to get the number of rows in the resultset.

$rows = mysql_num_rows($result);
if($rows == 0) {
   echo 'you have not ordered anything yet';
}
输什么也不输骨气 2024-12-21 17:23:09

使用 mysql_num_rows

<?php

$result = mysql_query("SELECT * FROM `order` WHERE username = '". $_SESSION['username']."' ")
or die(mysql_error()); ;

    if (mysql_num_rows($result) == 0) {
       echo 'you have not ordered anything yet';
    } else {
    echo "<table border='0'><table border width=100%><tr><th>Product</th><th>Quantity</th><th>Price</th><th>Date</th>";
    while($info = mysql_fetch_array($result))
    {
            echo "<tr>";
            echo "<td>" . $info['name']. "</td>";
            echo "<td>" . $info['quantity']. "</td>";
            echo "<td>" . $info['price']. "</td>";
            echo "<td>" . $info['date']. "</td>";
    
    }
    echo "</tr>";
    echo "</table>";
    }

Use mysql_num_rows

<?php

$result = mysql_query("SELECT * FROM `order` WHERE username = '". $_SESSION['username']."' ")
or die(mysql_error()); ;

    if (mysql_num_rows($result) == 0) {
       echo 'you have not ordered anything yet';
    } else {
    echo "<table border='0'><table border width=100%><tr><th>Product</th><th>Quantity</th><th>Price</th><th>Date</th>";
    while($info = mysql_fetch_array($result))
    {
            echo "<tr>";
            echo "<td>" . $info['name']. "</td>";
            echo "<td>" . $info['quantity']. "</td>";
            echo "<td>" . $info['price']. "</td>";
            echo "<td>" . $info['date']. "</td>";
    
    }
    echo "</tr>";
    echo "</table>";
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文