[英]HOW to echo New record with selected old record in PHP Mysql?
我是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";
我的查詢結果是
現在我想打印結果查詢的第一行,然后我成功。 但是現在我只想從第二行回ltitle
和Stitle
兩列。 在這里我失敗了。
這是我的代碼
<?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
}?>
我的代碼的結果是
在我的代碼的Previos列中,我要打印數據庫表的第二行數據。 您可以看到我的結果是錯誤的。 你們能幫我嗎?
用“ ORDER BY r.id DESC LIMIT 1,1”修改sql以直接獲取第二行:)
雅虎... !!! 我已經做了。 謝謝cedricliang對我的幫助
只需看下面我更新的代碼
<?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
}
}?>
我的代碼的結果是
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.