PHP and html n00b

Discussion in 'PHP' started by barffy, Aug 30, 2010.

  1. #1
    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>
     
    barffy, Aug 30, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    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 ?
     
    MyVodaFone, Aug 30, 2010 IP
  3. xcool101Man

    xcool101Man Guest

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you need to add <? before the <tr> that contains the data and to close it after the increment of i
     
    xcool101Man, Aug 30, 2010 IP
  4. barffy

    barffy Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    barffy, Aug 30, 2010 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    You dont need to do what @xcool101Man said so remove those, I would suggest you correct all your opening php tags to <?php
     
    MyVodaFone, Aug 30, 2010 IP
  6. xcool101Man

    xcool101Man Guest

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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
     
    xcool101Man, Aug 30, 2010 IP
  7. xcool101Man

    xcool101Man Guest

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    the problem is you cannot put html syntax in the php code directly like that without echo
     
    xcool101Man, Aug 30, 2010 IP
  8. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #8
    He did'nt have tags before you told him to put them there, your confusing him now.
     
    MyVodaFone, Aug 30, 2010 IP
  9. barffy

    barffy Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    barffy, Aug 30, 2010 IP
  10. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #10
    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.
     
    MyVodaFone, Aug 30, 2010 IP