Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay data from database into a table
    text
    copied!<p>I am trying to create very simple table with data from database in it. The table has two rows with three pieces of data in each (three columns).. I was playing around for couple hours now but can't get my head round it..I understand that $row['BandName'] needs to be indexed and increased each time the loop get round, but don't know how to attach index to it. I tried something like this: <code>$row['BandName'][$i]</code> but it displayed only one letter of full name (obviously it's not right) Any help would be appreciated..</p> <p>The table looks like this :</p> <pre><code>include 'db_constants.php'; $conn = sqlsrv_connect( $host, $cnct); if( $conn == false ) { echo "Could not connect.\n"; die( print_r( sqlsrv_errors(), true)); } else $tsql = "SELECT * FROM bands"; $stmt = sqlsrv_query( $conn, $tsql, array(), array( "Scrollable" =&gt; SQLSRV_CURSOR_KEYSET )); if( $stmt === false) { echo "Error in query preparation/execution.\n"; die( print_r( sqlsrv_errors(), true)); } else { $recordcount = sqlsrv_num_rows( $stmt );//count the records if($recordcount &gt;0) while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) { ?&gt; &lt;TR&gt; &lt;TABLE border="3" cellspacing="5" cellpadding="5"&gt; &lt;?php for($i=0; $i&lt;3; $i++) {?&gt; &lt;TD align="center" valign="top"&gt; &lt;table border="0" cellspacing="2" cellpadding="0"&gt; &lt;tr align="center"&gt; &lt;td valign="bottom"&gt;&lt;a href="details.php" target="_self"&gt;&lt;img src="band/1.jpg" width="140" height="110" border="0"&gt;&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr align="center"&gt; &lt;td class="greytablesm"&gt; &lt;?php echo $row['BandName'];?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/TD&gt; &lt;?php }?&gt; &lt;TR&gt; &lt;?php } else {echo "not found";} } ?&gt; </code></pre> <p>Table structure is </p> <pre><code>BandID - int autoincrement BandName - nvarchar(50) BandBio - nvarchar(max) Date - date START TABLE &lt;table&gt; loop from 1 to 2 &lt;tr&gt; loop from 1 to 2 &lt;td&gt; Show Details &lt;/td&gt; end loop &lt;/tr&gt; end loop END TABLE &lt;/table&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload