2013-02-08 63 views
0

我寫了一個簡單的php代碼來顯示mySql表中的數據。 當我使用「mysql_connect」,「mysql_select_db」時,這個工作正常,但當我使用 「mysqli_connect」,「mysqli_select_db」時不工作。任何人都可以請點亮它。mysqli不工作

我是一個非常初級的Web開發人員。

這是我寫的代碼:

<?php 
$con = mysqli_connect("localhost","root","abc123"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
} 

$db=mysqli_select_db("database_name",$con) or die("Could not select DB"); 

if (!$db) 
{ 
die('no database connected'); 
} 

$data = mysql_query("SELECT * FROM categories") 
or die(mysql_error()); 
Print "<table border cellpadding=3>"; 
while($info = mysql_fetch_array($data)) 
{ 
Print "<tr>"; 
Print "<th>Name:</th> <td>".$info['parentId'] . "</td> "; 
Print "<th>ID:</th> <td>".$info['title'] . "</td> </tr>"; 
} 
Print "</table>"; 
?> 

但是,如果我取代「mysqli的」支持「mysql」,那麼它的工作。

+3

混合mysqli_connect和mysqli_select_db用的mysql_query是你的問題....是關於你正在使用的庫一致......你需要使用mysqli的查詢方法 – 2013-02-08 23:09:25

回答

2

您在混合mysql_功能mysqli_。我會建議你看看documentation

<?php 
$link = mysqli_connect("localhost", "root", "abc123", "database_name"); 

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

$query = "SELECT * FROM categories"; 

if ($result = mysqli_query($link, $query)) { 

echo "<table border cellpadding=3>"; 

    /* fetch associative array */ 
    while ($info = mysqli_fetch_assoc($result)) { 
    echo "<tr>"; 
    echo "<th>Name:</th> <td>".$info['parentId'] . "</td> "; 
    echo "<th>ID:</th> <td>".$info['title'] . "</td> </tr>"; 
    } 

    /* free result set */ 
    mysqli_free_result($result); 

echo "</table>"; 
} 

/* close connection */ 
mysqli_close($link); 
?> 
+0

我複製此代碼並執行,仍然不工作。我必須在任何PHP config/ini文件中啓用任何內容嗎?我只是一個初級,而不是專家。我認爲PHP或mySql安裝中的設置有問題。 Pleese讓我知道。提前致謝。 – Isaac 2013-02-08 23:27:17

+0

@ user2055743 **什麼**不起作用?你有'error_reporting'啓用? – Kermit 2013-02-08 23:28:58