0

The following is my getdata.php script which isn't working:

 <?php
        $host="localhost";
        $username="root";
        $password="demo"; 
        $db_name="test";
        $tbl_name="test";

  mysql_connect("$host", "$username", "$password")or die("cannot connect");
  mysql_select_db("$db_name")or die("cannot select DB");

  $sql="SELECT * FROM $tbl_name";
  $result=mysql_query($sql);
  ?>

    <?php
        while($rows=mysql_fetch_array($result)){
             <table width="400" border="1" cellspacing="0" cellpadding="3">
                <tr>
                  <td width="10%"><? echo $rows['id']; ?></td>
                  <td width="30%"><? echo $rows['name']; ?></td>
                  <td width="30%"><? echo $rows['lastname']; ?></td>
                  <td width="30%"><? echo $rows['email']; ?></td>
                </tr>
             </table>
                  }
                  ?>

             <?php
                mysql_close();
             ?>

I’m using xampp server 1.8 with Windows XP sp2; I don't know which php version, but when I run this script it shows error '<' unexpected ~~~~~~~

Some error total thing is that this php is not executing tables and for per entry. What's wrong in my script?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Vikas Kandari
  • 1,612
  • 18
  • 23
  • 1
    Yeah, so you got some error, great description. The appropiate diagnosis: Something in your code is wrong. But it is nice to know you have xampp server 1.8, because in server 1.7 you have other errors. – Amarnasan Nov 11 '15 at 11:06
  • yes but i'm not infront of my pc i can't tell error but i can give some hints like Error unexpected ">" at line somthing line so problem is that php is not able to execute html elements – Vikas Kandari Nov 11 '15 at 11:08
  • Please don't use the `mysql_` database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use `mysqli` or `PDO` extensions instead. And this is [why you shouldn't use `mysql_` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Rajdeep Paul Nov 11 '15 at 11:12
  • Sir i got this error on my script {Parse error: syntax error, unexpected '<' in /home/vhosts/useme.freevar.com/x.php on line 21} – Vikas Kandari Nov 11 '15 at 11:14
  • `while($rows=mysql_fetch_array($result)){` close the php tag `?>` and maybe insted of `` shortcode use ` }` reopen php tag – Mujnoi Gyula Tamas Nov 11 '15 at 11:28

3 Answers3

2

You're getting an error because you are trying to put HTML code directly into PHP without quoting or echoing which is what PHP is trying to tell you with 'Unexpected <'.

Your issue is here:

while($rows=mysql_fetch_assoc($result)){
  <table width="400" border="1" cellspacing="0" cellpadding="3">
    <tr>
      <td width="10%"><? echo $rows['id']; ?></td>
      <td width="30%"><? echo $rows['name']; ?></td>
      <td width="30%"><? echo $rows['lastname']; ?></td>
      <td width="30%"><? echo $rows['email']; ?></td>
    </tr>
  </table>
}

What you need to do instead is (if you wanted this on the page) add an echo statement and encapsulate your HTML code in quotes like this:

while($rows=mysql_fetch_assoc($result)){
  echo "
  <table width='400' border='1' cellspacing='0' cellpadding='3'>
    <tr>
      <td width='10%'>".$rows['id']."</td>
      <td width='30%'>".$rows['name']."</td>
      <td width='30%'>".$rows['lastname']."</td>
      <td width='30%'>".$rows['email']."</td>
    </tr>
  </table>";
}

All we do here is concatenate our PHP variables to the string. As far as I am aware you could also do it like this using the alternative syntax:

while($rows=mysql_fetch_assoc($result)):?>
  <table width="400" border="1" cellspacing="0" cellpadding="3">
    <tr>
      <td width="10%"><? echo $rows['id']; ?></td>
      <td width="30%"><? echo $rows['name']; ?></td>
      <td width="30%"><? echo $rows['lastname']; ?></td>
      <td width="30%"><? echo $rows['email']; ?></td>
    </tr>
  </table>
<?php
endwhile;

It's also worth noting that MYSQL_* functions are now deprecated. If you can, you might want to consider using the MYSQLI functions instead.

Henders
  • 1,195
  • 1
  • 21
  • 27
  • Thank you Sir My Problem is Resolved By your Code echo "elements of html" – Vikas Kandari Nov 11 '15 at 11:18
  • Sir error is syntax error, unexpected 'hello' (T_STRING), expecting ',' or ';' in /home/vhosts/useme.freevar.com/x.php on line 24 – Vikas Kandari Nov 11 '15 at 11:23
  • This is a different issue because this code doesn't contain the word 'hello'. As a guess, check your code for more situations where you have not wrapped HTML in quotes. You are more than likely doing something like `if(true){ hello }` which is not valid syntax. Instead, like this issue, you need to do `if(true){ echo 'hello'; }` instead. – Henders Nov 11 '15 at 11:25
  • No sir i put hello on then i run php and i got this error {syntax error, unexpected 'hello' (T_STRING), expecting ',' or ';' in /home/vhosts/useme.freevar.com/x.php on line 24 } – Vikas Kandari Nov 11 '15 at 11:26
  • Watch your quotes. If you choose my first solution you'll either need to escape the quotes like this: `` or you'll need to use single quotes like this: ``. The second solution may work better in this circumstance. – Henders Nov 11 '15 at 11:28
  • Thank you sir i will try this also – Vikas Kandari Nov 11 '15 at 11:32
1

PHP tags not handled properly, close it and open it appropriately.

Just add of your code like see below -

PHP tags not handled properly, close it and open it appropriately.

Just add of your code like see below -

<?php

    $host="localhost";
    $username="root";
    $password="demo"; 
    $db_name="test";
    $tbl_name="test";

    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);

    while($rows=mysql_fetch_array($result)){ 
  // here is the change
  ?>
         <table width="400" border="1" cellspacing="0" cellpadding="3">
          <tr>
              <td width="10%"><?php echo $rows['id']; ?></td>
              <td width="30%"><?php echo $rows['name']; ?></td>
              <td width="30%"><?php echo $rows['lastname']; ?></td>
              <td width="30%"><?php echo $rows['email']; ?></td>
          </tr>
          </table>
<?php      
     } // endwhile

     mysql_close();
 ?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Shaymol Bapary
  • 468
  • 3
  • 11
0

To prevent future code errors is recommended to use mysqli dbase objetct. The code is as that:

$host="localhost";
$username="root";
$password="demo"; 
$db_name="test";
$tbl_name="test";

$mysqli = new mysqli("localhost",$username,$password,$db_name);

$sql="SELECT * FROM $tbl_name";

$res=$mysqli->query($sql);


    while($rows=$res->fetch_assoc()){ ?>
         <table width="400" border="1" cellspacing="0" cellpadding="3">
            <tr>
              <td width="10%"><? echo $rows['id']; ?></td>
              <td width="30%"><? echo $rows['name']; ?></td>
              <td width="30%"><? echo $rows['lastname']; ?></td>
              <td width="30%"><? echo $rows['email']; ?></td>
            </tr>
         </table>
   <?php }
         $res->close();
  ?>

PD: be carefull, you are generating one table for each register, I think you want to create one row for each register

JRHuasi
  • 9
  • 3