yasser h yasser h - 2 years ago 64
PHP Question

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.

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

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

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

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


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:

<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

I don't know if i should modify this code or use a new one .thanks for the help.

Answer Source

If you need to show only one record then you can store the values in array first and later the display it on browser.


$email = array();
while($row = mysql_fetch_array($retval, MYSQL_NUM)) { 
echo "<table>";
foreach ($email as $key => $value) {
    echo "<tr>";
    echo "<td>$value</td>";
    echo "</tr>";
echo "</table>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download