使用mysqli使用php连接数据库

使用mysqli使用php连接数据库,php,mysqli,Php,Mysqli,它不起作用了。我使用的代码是 <?php $conn=mysqli_connect('127.0.0.1','root',''); if(!$conn) { echo "Connection Not Established"; } if(!mysqli_select_db($conn,'lpractice')) { echo "Databa

它不起作用了。我使用的代码是

    <?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>";
    ?>

mysqli_fetch_数组
not
mysql_fetch_数组
,您缺少一个
在您的
echo”“

之后,我认为根本问题与mysql无关。如果您的输出在
-标记之后开始,那么您的代码似乎不是由php解释器运行的。例如,当您直接使用浏览器访问php文件时,就会发生这种情况

使用php的通常设置是Web服务器。您可以使用几种解决方案。例如,有一个简单易用的Web服务器,用于您自己的计算机进行测试和开发。根据您的操作系统,可能会有更好的解决方案(例如linux或mac上的预配置包)。

//试试这个

对所有mysql语法使用
mysqli
。只有
mysql!=mysqli
可能与@Script47重复—它不是这样的重复。当前,php解释器未运行,此错误将在运行后发生。如果显示源代码,您将看到所有php代码。您必须按照sauerburger在回答中的建议,在web服务器上正确配置PHP解释器。
"; while($row= mysql_fetch_array($res)) { echo ""; echo "" . $row['name'] . ""; echo "" . $row['email'] . ""; echo "" . $row['age'] . ""; echo "" . $row['sex'] . ""; echo "" } echo ""; ?>
//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();
?>