简体   繁体   English

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

[英]HOW to echo New record with selected old record in PHP Mysql?

I am new in PhP. 我是PhP新手。 I have a query 我有一个查询

"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";

Result of My query is 我的查询结果是

询问

Now i want to print first row of of resulted query and i success. 现在我想打印结果查询的第一行,然后我成功。 But now i want to echo only two columns ltitle and Stitle from second row. 但是现在我只想从第二行回ltitleStitle两列。 Here i failed. 在这里我失败了。

Here is my code 这是我的代码

<?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 of my code is 我的代码的结果是

结果

In Previos column of my code i want to print second row data of my db table. 在我的代码的Previos列中,我要打印数据库表的第二行数据。 You can see my result is wrong. 您可以看到我的结果是错误的。 Can you guys please help me? 你们能帮我吗?

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

Yahoo...!!! 雅虎... !!! I have done it. 我已经做了。 Thank You cedricliang for my help 谢谢cedricliang对我的帮助

Just look at my updated code below 只需看下面我更新的代码

<?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";


$sql1= "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,1";

$result = $conn->query($sql);

$result1 = $conn->query($sql1);
//$array = array('1','2','3');

while ($row = $result->fetch_assoc()){
    while ($row1 = $result1->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 $row1['ltitle'] ?>)</td> </tr></td>
        </tr>

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


</table>

</body>
</html>

<?php
    }
}?>

And the result of my code is 我的代码的结果是

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM