Hi, Im just starting to learn to work with php and SQL and im having a abit of trouble with some code. i know its going to be a simple tweek for you guys and i appologise but im learning! Basically with this code all i can get to output is the title "Database Output" and Name Mobile Fax etc with no data being pulled from my database. I would be extremely gratefull if someone could point me in the right direction in order to further my knowledge of this language! Im certain its not an issue with the connection to the data base as if i take the table formatting out its prints as a list. Regards Barffy <html> <head></head> <body> <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $query="SELECT * FROM contacts "; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "<b><center>Database Output</center></b><br><br>"; ?> <table border ="0" cellspacing ="2" cellpadding ="1"> <tr> <th><font face ="Verdana, Arial, sans-serif">Name</font></th> <th><font face ="verdana, Arial, sans-serif">Phone</font></th> <th><font face ="Verdana, Arial, sans-serif">Mobile</font></th> <th><font face ="Verdana, Arial, sans-serif">Fax</font></th> <th><font face ="Verdana, Arial, sans-serif">Email</font></th> <th><font face ="Verdana, Arial, sans-serif">Web</font></th> </tr> <? $i=0; while ($i < $num) { $first=mysql_result($result,$i,"first"); $last=mysql_result($result,$i,"last"); $phone=mysql_result($result,$i,"phone"); $mobile=mysql_result($result,$i,"mobile"); $fax=mysql_result($result,$i,"fax"); $email=mysql_result($result,$i,"email"); $web=mysql_result($result,$i,"web"); ?> <tr> <td><font face ="Verdana, Arial, sans-serif"><?echo $first."".$last; ?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $phone; ?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $Mobile; ?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $fax; ?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $fax;?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $email;?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $web;?></font></td> </tr> <? $i++; } </body> </html>
Just at a glance, your missing your closing php tag at the end : ?> <? $i++; }?> </body> </html> PHP: Its also no harm to open your php tags with <?php instead of just <? Apart from that are you receiving any errors ?
Hi, Thank you for the replies so far. I have made the changes suggested and now rather than display the title and headings, it throws up the error: Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\xampp\LearningPHP\output.php on line 53 I will again show you the code with the edits made as it stands to good reason i have added the <?php tags in the wrong place! <html> <body> <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $query="SELECT * FROM contacts "; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "<b><center>Database Output</center></b><br><br>"; ?> <table border ="0" cellspacing ="2" cellpadding ="1"> <tr> <th><font face ="Verdana, Arial, sans-serif">Name</font></th> <th><font face ="verdana, Arial, sans-serif">Phone</font></th> <th><font face ="Verdana, Arial, sans-serif">Mobile</font></th> <th><font face ="Verdana, Arial, sans-serif">Fax</font></th> <th><font face ="Verdana, Arial, sans-serif">Email</font></th> <th><font face ="Verdana, Arial, sans-serif">Web</font></th> </tr> <?php $i=0; while ($i < $num) { $first=mysql_result($result,$i,"first"); $last=mysql_result($result,$i,"last"); $phone=mysql_result($result,$i,"phone"); $mobile=mysql_result($result,$i,"mobile"); $fax=mysql_result($result,$i,"fax"); $email=mysql_result($result,$i,"email"); $web=mysql_result($result,$i,"web"); ?> <?php <tr> <td><font face ="Verdana, Arial, sans-serif"><?echo $first."".$last; ?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $phone; ?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $Mobile; ?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $fax; ?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $fax;?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $email;?></font></td> <td><font face ="Verdana, Arial, sans-serif"><?echo $web;?></font></td> </tr> ?> <?php $i++; } ?> </body> </html> Once again thank you for your time and assistance.
You dont need to do what @xcool101Man said so remove those, I would suggest you correct all your opening php tags to <?php
sorry I couldn't explain correctly what I meant earlier please remove the <?php before the <tr> tags that has the information and the ?> after it
Hi, Tags are now removed and all corrected to <?php error is now : Parse error: syntax error, unexpected $end in C:\xampp\htdocs\xampp\LearningPHP\output.php on line 73 I thought PHP and html were fine to go together? This is the original code i wrote which i am trying to modify to improve the visual appearance using html. This code works fine. I dont know whether this will clear up anything? <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $query="SELECT * FROM contacts "; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $first=mysql_result($result,$i,"first"); $last=mysql_result($result,$i,"last"); $phone=mysql_result($result,$i,"phone"); $mobile=mysql_result($result,$i,"mobile"); $fax=mysql_result($result,$i,"fax"); $email=mysql_result($result,$i,"email"); $web=mysql_result($result,$i,"web"); echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>"; $i++; } ?> Thanks guys.
Go back to your first code and do as I suggested, if it doesn't work by all means reply, but please don't edit your code with something else and then expect it to work.