misfitxnet Posted June 21, 2009 Share Posted June 21, 2009 If i am writing a script in which I connect to a database and define some elements such as <?php $connect=mysql_connect('host', 'username', 'password'); $db=mysql_select_db('db'); if($!db){ exit(mysql_error) } if($!connect){ exit(mysql_error) } $sql="SELECT * FROM table ORDER by id DESC"; ?> then I write some HTML, and then create a new part of the the page which will display the sql data in a table. <?php while ($row=mysql_fetch_array($sql)){ <tr> <td> <? echo $row['name']; ?> </td> </tr> ?> and this does not work... however, a "while ($row=mysql_fetch_array($sql)){ echo $row['name'];" in the first part works. My question is... what is the syntax regarding where a new <?php section must begin??? When won't php recognize HTML and vice versa???? Link to comment https://forums.phpfreaks.com/topic/163082-starting-new-php-functions/ Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 Don't use PHP short tags. Change <? to <?php. Link to comment https://forums.phpfreaks.com/topic/163082-starting-new-php-functions/#findComment-860487 Share on other sites More sharing options...
Andy-H Posted June 21, 2009 Share Posted June 21, 2009 Also, use code tags around your code, it makes it more readable and has syntax highlighting. [php ]Your php code here[/php ] Link to comment https://forums.phpfreaks.com/topic/163082-starting-new-php-functions/#findComment-860489 Share on other sites More sharing options...
Andy-H Posted June 21, 2009 Share Posted June 21, 2009 <?php $connect=mysql_connect('host', 'username', 'password'); $db=mysql_select_db('db'); if($!db){ exit(mysql_error) } if($!connect){ exit(mysql_error) } $sql="SELECT * FROM table ORDER by id DESC"; ?> then I write some HTML, and then create a new part of the the page which will display the sql data in a table. <?php while ($row=mysql_fetch_array($sql)) { // break out of PHP tags to output some html ?> <tr> <td> <?php // add the dynamic content echo $row['name'] . "\n"; ?> </td> </tr> <?php // close brace } ?> Link to comment https://forums.phpfreaks.com/topic/163082-starting-new-php-functions/#findComment-860492 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.