-4

It's not working. The code i used is

    <?php
        $conn=mysqli_connect('127.0.0.1','root','');
        if(!$conn)
        {
            echo "Connection Not Established";
        }
        if(!mysqli_select_db($conn,'lpractice'))
        {
            echo "Database Not Found!";
        }
        $res=mysqli_query($conn,"select * from signup");
        echo "<table>";
            while($row= mysql_fetch_array($res))
            {
                echo "<tr>";
                echo "<td>" . $row['name'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
                echo "<td>" . $row['age'] . "</td>";
                echo "<td>" . $row['sex'] . "</td>";
                echo "</tr>"

            }
        echo "</table>";
    ?>

And the output coming is

"; while($row= mysql_fetch_array($res)) { echo ""; echo "" . $row['name'] . ""; echo "" . $row['email'] . ""; echo "" . $row['age'] . ""; echo "" . $row['sex'] . ""; echo "" } echo ""; ?>
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • use `mysqli` for all mysql syntax .you are missing to some syntax in `mysql` only – prasanth Aug 16 '17 at 08:06
  • `mysql != mysqli` – Script47 Aug 16 '17 at 08:07
  • 1
    Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Script47 Aug 16 '17 at 08:07
  • @Script47 It is not such a duplicate. Currently, the php interpreter is not running, this error will occur after. If you display source code, you will see all your php code. You have to configure PHP interpreter correctly on your web server as suggested by sauerburger in his answer. – Alexandre Tranchant Aug 16 '17 at 08:39

3 Answers3

1

mysqli_fetch_array not mysql_fetch_array and you're missing a ; after your echo "</tr>"

Bananaapple
  • 2,984
  • 2
  • 25
  • 38
1

I think the fundamental problem is not related to mysql. If your output starts after the <table>-tag it looks like your code has not been run by the php interpreter. This happens, for example, when you access the php file directly with your browser.

The usual setup in order to use php is a webserver. There are several solutions you can use. For example, there is xampp which is an easy to use webserver for your own computer for testing and development. Depending on your OS there might are better solutions (such as pre-configured packages on linux or mac).

sauerburger
  • 4,569
  • 4
  • 31
  • 42
0
//Try this

<?php
#create connection
$host_name = "127.0.0.1";
$username = "root";
$password = "";
$database_name = "lpractice";
$connection = mysqli_connect("$host_name","$username","$password","$database_name");

#check connection
if(mysqli_connect_error()){
    echo "Connection Not Established. " .mysqli_connect_error();
    }
$query = "SELECT * FROM signup";
$result = $connection->query($query);
if ($result->num_rows > 0) {
  //echo table header
    echo"
      <table>
        <thead>
           <tr>
             <th>Name</th>
             <th>Email</th>
             <th>Age</th>
             <th>Sex</th>
           </tr>
        </thead>
      ";
while($row = $result->fetch_assoc()) {
// output data of each row
  echo"<tbody>";
    echo"<tr>";
      echo "<td>" . $row['name'] . "</td>";
      echo "<td>" . $row['email'] . "</td>";
      echo "<td>" . $row['age'] . "</td>";
      echo "<td>" . $row['sex'] . "</td>";
    echo"</tr>";
  echo"</tbody>";
echo"</table>";
  }
} else {
    echo "No records found.";
}
$connection->close();
?>
Abdoulie Kassama
  • 782
  • 10
  • 20