簡體   English   中英

為什么此php和MySQL不顯示結果

[英]Why is this php and MySQL not displaying results

我目前正在嘗試連接到數據庫,並在表“ STUDENTS”中顯示所有學生的名字。 我的連接似乎很好,因為ping()似乎正常。 我沒有收到任何錯誤消息,但是查詢/顯示結果似乎不起作用。 誰能幫我解決這個問題?

  <?php
    $user = 'root';
    $password = 'root';
    $db = 'gradebook';
    $host = 'localhost';
    $port = 8889;

    $link = mysqli_init();
    $connection = mysqli_real_connect($link, $host, $user, $password, $db, $port)
        or die("Cannot connect to database server.");

    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    /* check if server is alive */
    if (mysqli_ping($link)) {
        printf ("Our connection is ok!\n </br>");
    } else {
        printf ("Error: %s\n", mysqli_error($link));
    }

    $query = "SELECT * FROM STUDENTS";

    $statement = $connection->prepare($query);
    $statement->execute();
    $statement->bind_result($First_name);
    while ($statement->fetch()) {
        print $First_name . '</br>';
    }

    mysqli_close($link);

    ?>

請先打開錯誤報告。

然后-仔細閱讀mysqli-manuals。

$link = mysqli_init(); 返回對象。 mysqli_real_connect返回bool

而且您正在嘗試對bool變量執行prepare方法:

$statement = $connection->prepare($query);

當然,這是行不通的。 如果你有錯誤報告on -你會看到相關的錯誤信息。 因此,假設其他一切都很好:

$statement = $link->prepare($query);  // NOT $connection
$statement->execute();
$statement->bind_result($First_name);
while ($statement->fetch()) {
    print $First_name . '</br>';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM