2016-06-08 69 views
0

進出口運行一個LAMP服務器和IM試圖在網頁上顯示我的數據庫的表PHP連接並顯示MySQL表 - LAMP服務器

這是我的PHP腳本

$servername = "127.0.0.1"; 
$username = "root"; 
$password = "none"; 
$dbname = "chegada"; 

$conn = mysqli_connect($servername, $username, $password, $dbname); 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
}else{echo "OK!";} 

$sql = "SHOW TABLES FROM $dbname"; 
$result = mysql_query($sql); 
if (!$result) { 
    echo "DB Error, could not list tables\n"; 
    echo 'MySQL Error: ' . mysql_error(); 
    exit; 
} 

以及網頁顯示這

Warning: mysqli_connect(): (HY000/2002): Connection refused in /var/www/html/index.php on line 16

Notice: Trying to get property of non-object in /var/www/html/index.php on line 17

OK!

Fatal error: Uncaught Error: Call to undefined function mysql_query() in /var/www/html/index.php:22 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 22

有人可以幫我這個嗎?

+1

將mysqli與mysql混合使用 – Saty

+0

不要使用不推薦使用的'mysql_ *'函數,而是使用MySQLi/PDO。 – Raptor

+0

$ password =「」; – JYoThI

回答

0

試試這個

$servername = "127.0.0.1"; 
$username = "root"; 
$password = ""; // maybe this should be an empty string if you dont have password setup 
$dbname = "chegada"; 

$conn = mysqli_connect($servername, $username, $password, $dbname); // this should be mysqli_query 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
}else{echo "OK!";} 

$sql = "SHOW TABLES FROM $dbname"; 
$result = mysqli_query($conn,$sql); 
if (!$result) { 
    echo "DB Error, could not list tables\n"; 
    echo 'MySQL Error: ' . mysqli_error(); 
    exit; 
} else { 
    $res = mysqli_fetch_all($result); 
    var_dump($res); 
} 
+0

謝謝,但我只得到OK!在頁面上,仍然沒有打印表格,我想我得到了1º錯誤,因爲當我嘗試登錄mysql時,它給了我錯誤2002(...)@erwan – n0t313

+0

我編輯了代碼,以便$ res變量應該包含你所期望的, – erwan

+0

我在connect.php中有這個: $ servername =「127.0.0.1」; $ username =「root」; $ password =「none」; $ dbname =「chegada」; $ conn = mysqli_connect($ servername,$ username,$ password, $ dbname); //這應該是mysqli_query 如果($ conn-> connect_error){ 模具( 「連接失敗」。$ conn-> connect_error);} ,並得到了查詢離子的index.php 它仍然不展示 – n0t313

0

使用mysqli_query

$result = mysqli_query($conn,$sql); 
0

我supect你有一個默認的本地MySQL,在這種情況下,密碼爲 「無」 是一個空字符串。從字面上看,沒有密碼,而不是「無」。你需要:

$servername = "127.0.0.1"; 
$username = "root"; 
$password = ""; 
$dbname = "chegada"; 

你的第二個錯誤是由你在mysqli_中開始的事實造成的,但是後來恢復到了mysql_的一半。

相關問題