php - 如何在 PHP Mysql 中回显新记录与选定的旧记录?

标签 php mysql database echo

我是 PhP 新手。我有一个疑问

"SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as cname,t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle
FROM og_ratings r 
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
WHERE c.id= 338
ORDER BY r.id DESC
LIMIT 2";

我的查询结果是

query

现在我想打印结果查询的第一行并且我成功了。但现在我只想回显第二行中的两列 ltitleStitle 。在这里我失败了。

这是我的代码

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pacra1";


$conn = new mysqli($servername, $username, $password, $dbname);
//$id2 = $_GET['id'];
$sql= "SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as cname,t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle
FROM og_ratings r 
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
WHERE c.id= 338
ORDER BY r.id DESC
LIMIT 1";
$result = $conn->query($sql);
//$array = array('1','2','3');

while ($row = $result->fetch_assoc()){

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<table border="1">
        <tr>
            <td> ID </td>
            <td> <?php echo $row['client_id'] ?> </td>
        </tr>

        <tr> 
            <td>Name </td>
            <td><?php echo $row['cname'] ?> </td>
        </tr>

        <tr>
            <td>Rating Type </td>
            <td><?php echo $row['ttitle'] ?> </td>
        </tr>
        <tr>
            <td>Action </td>
            <td><?php echo $row['atitle'] ?> </td>
        </tr>
        <tr>
            <td>Outlook </td>
            <td><?php echo $row['otitle'] ?></td>
        </tr>
        <tr>
            <td rowspan="2">Long Term Rating </td>
            <td>Current (<?php echo $row['ltitle'] ?>) <tr><td>Previous (<?php echo $row['ltitle'][0] ?>)</td> </tr></td>
        </tr>

        <tr>
            <td rowspan="2">Short Term Rating </td>
            <td>Current (<?php echo $row['stitle'] ?>) <tr><td>Previous (<?php echo $row['stitle'][0] ?>)</td> </tr></td>
        </tr>


</table>

</body>
</html>

<?php
}?>

我的代码的结果是

result

在我的代码的 Previos 列中,我想打印数据库表的第二行数据。 你可以看到我的结果是错误的。你们能帮帮我吗?

最佳答案

用“ORDER BY r.id DESC LIMIT 1,1”修改sql,直接获取第二行:)

关于php - 如何在 PHP Mysql 中回显新记录与选定的旧记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31830360/

相关文章:

php - 列表页面上的 Magento 自定义过滤器提供了不正确的产品数量

javascript - 单击父 li 时如何设置打开下拉菜单列表然后使用 javascript 打开子 li?

java - JPA + MySQL - 数据库生成的列值

python - 将值保存到数据库 Django

php - php 中只有没有字段的值

php - woocommerce 阻止结帐提交付款方式更改

php - Facebook API - 上传图片后获取图片 URL

mysql - 表示mySQL中两个用户之间的兼容性

mysql - 影响 MySQL 中带有关键字的所有行的产品变体

MySQL LIKE 和 NOT LIKE 搜索同一列