2017-04-21 189 views
1

我需要一些幫助,這表明我有我的數據庫中的數據,但我不能看到能夠to.`需要幫助,以顯示從MySQL數據庫中的數據

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

我很新的這所以我可能錯過了一些簡單的東西。任何幫助讚賞。

+0

的可能的複製[我可以在PHP MySQL的混合的API?](http://stackoverflow.com/questions/17498216/can-i-mix -mysql -apis-in-php) – Qirel

+0

'mysql_ *'不適用於'mysqli_ *' - 蘋果和桔子。此外,失去第一個'$ row = mysql_fetch_array($ result);' - 你沒有做任何事情,並且第一個'mysqli_master_query()' – Qirel

回答

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.phphttps://www.w3schools.com/php/php_mysql_insert.asp