如何用php显示mysql表?

ybzsozfc  于 2022-10-22  发布在  PHP
关注(0)|答案(1)|浏览(159)

我对mysql相对较新,我已经按照教程来显示我的数据库。我使用了PHP,我找不到任何错误,但它无法显示。有人能帮忙吗?
谢谢
服务器的名称为“sql306.iceiy.com”
行的名称为“Exercise_name”、“Muscle_name”和“PPS”

$servername = "sql306.iceiy.com";
        $username = "icei_32733650";
        $password = "ZoD473049gais";

        $conn = new mysqli($servername, $username, $password);

        // Check connection
        if ($conn->connect_error) {
          echo("Connection failed: " . $conn->connect_error);
        }
        echo "Connected!";

        $sql = "SELECT Exercise_Name, Muscle_Name, PPS, FROM gym_exercises";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "Table";
            echo "<tr>";
                echo "<th>Exercise_Name</th>";
                echo "<th>Muscle_Name</th>";
                echo "<th>PPS</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" . $row['Exercise_Name'] . "</td>";
                echo "<td>" . $row['Muscle_Name'] . "</td>";
                echo "<td>" . $row['PPS'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR:";
}

// Close connection
mysqli_close($link);
      ?>
h79rfbju

h79rfbju1#

我看到您的代码有几个问题:
1.您需要指定正在处理的关系所在的数据库:$conn = new mysqli($servername, $username, $password, $databaseName);
1.select语句中有一个额外的逗号。
1.由于您在连接时使用了面向对象的样式,因此应继续使用该样式以保持一致性。此外,我不知道变量$link在哪里定义。
1.您没有正确的****标记。
在下面的代码中,我消除了对echo的调用。这只是一个问题,什么更整洁、更清晰。
此外,如果任何数据包含“<”和/或“>”,表中的值可能会被误认为是HTML标记。如果是这样,那么您将需要用HTML实体替换这些字符,例如htmlentities(row['PPS'])

<?php

$servername = "sql306.iceiy.com";
$username = "icei_32733650";
$password = "ZoD473049gais";
$databasename = "some_database_name";

$conn = new mysqli($servername, $username, $password, $databasename);

// Check connection
if ($conn->connect_error) {
    echo("Connection failed: " . $conn->connect_error);
    return; /* Do not execute remaining code */
}

//echo "Connected!"; /* do not put this out */

$sql = "SELECT Exercise_Name, Muscle_Name, PPS FROM gym_exercises";
if($result = $conn->query($sql)) {
    if($result->num_rows > 0) {
?>
<table>
    <tr>
        <th>Exercise_Name</th>
        <th>Muscle_Name</th>
        <th>PPS</th>
    </tr>
<?php
        while ($row = $result->fetch_array()) {
?>
    <tr>
        <td><?=$row['Exercise_Name']?></td>
        <td><?=$row['Muscle_Name']?></td>
        <td><?=$row['PPS']?></td>
    </tr>
<?php
        }
?>
</table>
<?php
    }
    else {
        echo "No records matching your query were found.";
    }
    // Free result set
    $result->free();
}
else {
    echo "ERROR";
}

// Close connection
$conn->close();

相关问题