简体   繁体   English

显示数据库中的一行并移至下一列

[英]Display one row from database and move to the next column

in my database table (id , book , cover , date , bookname .....) i used this method to display the row content of each column and it's working fine. 在我的数据库表(id,book,cover,date,bookname .....)中,我使用此方法显示每列的行内容,并且它工作正常。

<?php    
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
    die('Could not connect: ' . mysql_error());
}
$sql = "SELECT  * FROM books  ";
mysql_select_db('x');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
    die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_NUM)) { ?>

    <table>
    <tr>
    <td width="40%"><?php echo $row[0];?></td>
    <td width="40%"><?php echo $row[1];?></td>
    <td width="20%"><?php echo $row[2];?></td>
    </tr>
    </table>
<?
}  

mysql_free_result($retval);
mysql_close($conn);

But this time i want to display in each one row (like:$row[0]) and in the next also $row[0] but for the next column.example: 但这次我想在每一行显示(如:$ row [0]),在下一行也显示$ row [0],但是对于下一列。示例:

<table>
    <tr>
          <td width="40%"><?php echo $row[0];?></td>    //column1
          <td width="40%"><?php echo $row[0];?></td>    //column2
          <td width="20%"><?php echo $row[0];?></td>    //column3
    </tr>
</table>

I don't know if i should modify this code or use a new one .thanks for the help. 我不知道我是否应修改此代码或使用新代码。谢谢你的帮助。

If you need to show only one record then you can store the values in array first and later the display it on browser. 如果只需显示一条记录,则可以先将值存储在数组中,然后再将其显示在浏览器中。

<?php

$email = array();
while($row = mysql_fetch_array($retval, MYSQL_NUM)) { 
    $email[]=$row[0];
}
echo "<table>";
foreach ($email as $key => $value) {
    echo "<tr>";
    echo "<td>$value</td>";
    echo "</tr>";
}
echo "</table>";
?>

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

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