yasser h yasser h - 7 months ago 9
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.

<?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:

<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.

Answer

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