2017-07-03 228 views
-4

所以我做了我的第一個數據庫在線。我用phpmyadmin,我創建了表和用戶。
現在我想在我的網站的某個頁面上顯示該表格,並且可以讓用戶從該網站編輯數據庫。
我的問題是數據庫無法正常工作:它沒有連接。我不知道該怎麼做。
我的數據庫稱爲letstenf_santi和我的表passeggeri。 這是我試圖用來在網站上顯示錶格的代碼。MySQL似乎沒有工作

<?php 
//establishing connection 
mysql_connect('localhost', 'root', ''); 
//selecting a database 
mysql_select_db('letstenf_santi'); 
$sql = 'SELECT * FROM `letstenf_santi`.`passeggeri`'; 
$records=mysql_query($sql); 
?> 

<html> 
<head> 
    <title>mostra</title> 
</head> 
<body> 
<table width="300" border="1" cellpadding="10" cellspacing="1"> 
<tr> 
<th>id pass</th> 
<th>nome</th> 
<th>eta</th> 
<th>sesso</th> 
</tr> 
<?php 
while($pass=mysql_fetch_assoc($records)){ 
    echo "<tr>"; 
echo "<td>".$pass['idpasseggero']."</td>"; 
echo "<td>".$pass['nome']."</td>"; 
echo "<td>".$pass['eta']."</td>"; 
echo "<td>".$pass['sesso']."</td>"; 
echo "</tr>"; 
} 
?> 
</table> 
</body> 
</html> 
+3

棄用語法檢測 –

+0

什麼是你得到的錯誤? –

+0

無。它只是不起作用。我不知道該怎麼辦 – gab

回答

0

取而代之, 的mysql_connect( '本地主機', '根', ''); mysql_select_db('letstenf_santi');

你可以試試這個,

$連接=的mysql_connect( 'localhost' 的 '根', '');

$ db = mysql_select_db('letstenf_santi',$ connection);

+0

仍然不起作用:/ – gab

+0

我可能知道您收到的錯誤是什麼? –

+0

請把這個「mysql_fetch_assoc」改成這個「mysql_fetch_array」 –

0

試試這個代碼

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "letstenf_santi";//your db name 

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

$sql = "SELECT * FROM tablename";//replace table name with your table name 
$result = mysqli_query($conn, $sql); ?> 
<html> 
<head> 
    <title>mostra</title> 
</head> 
<body> 
<table width="300" border="1" cellpadding="10" cellspacing="1"> 
<tr> 
<th>id pass</th> 
<th>nome</th> 
<th>eta</th> 
<th>sesso</th> 
</tr> 
<?php if (mysqli_num_rows($result) > 0) { 
    // output data of each row 
    while($row = mysqli_fetch_assoc($result)) { 
     echo "<tr>"; 
echo "<td>".$row['idpasseggero']."</td>"; 
echo "<td>".$row['nome']."</td>"; 
echo "<td>".$row['eta']."</td>"; 
echo "<td>".$row['sesso']."</td>"; 
echo "</tr>"; 
    } 
} else { 
    echo "0 results"; 
} 

mysqli_close($conn); 
?> 
</table> 
</body> 
</html> 
+0

它的工作原理!非常感謝 – gab