2015-12-09 66 views
0

我目前正試圖連接到一個數據庫並在表格「學生」中顯示所有學生的名字。我好像連接好,因爲我的ping()似乎很好。我沒有收到任何類型的錯誤消息,但是我的查詢/顯示結果似乎不起作用。任何人都可以幫我解決這個問題嗎?爲什麼這個php和MySQL不顯示結果

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

    ?> 
+0

沒有'mysqli_fetch'功能。它是'fetch()'。 –

+0

即使在使用fetch()時也不起作用.. –

+0

您是否看到'我們的連接正常!'? –

回答

2

請打開錯誤報告開始。

然後 - 仔細閱讀mysqli-manuals。

$link = mysqli_init();返回對象。 mysqli_real_connect返回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>'; 
} 
相關問題