需要帮助来显示来自mysql数据库的数据

时间:2017-04-21 17:07:39

标签: php html mysql database

我需要一些帮助来显示我在数据库中的数据,但我无法看到它。

    $servername = "servername";
    $username = "username";
    $password = "password";
    $dbname = "dbname";

    $connect = mysqli_connect($servername, $username, $password, $dbname) or die ("connection failed");

    //Query

    $query = "SELECT * FROM 'Students'";
    mysqli_master_query($dbname, $query) or die ("Error while Query"); 

    $result = mysqli_master_query($dbname, $query);
    $row = mysql_fetch_array($result);

    while ($row = mysql_fetch_array($result)) {
    echo "<p>".$row['Name']."</p>";
    };

    mysql_close($connect);
?>` 

我对此很陌生,所以我可能错过了一些简单的事情。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

下面是连接数据库并从中选择数据的常规过程的示例代码。请遵循这种类型的编码,因为现在不推荐使用MySQL并使用MySQLi。

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>

如需进一步参考,请查看http://php.net/manual/en/book.mysqli.php以及https://www.w3schools.com/php/php_mysql_insert.asp