嗨我有問題訪問我的服務器我在我的筆記本電腦上使用mysql apache2和php。我的問題是我可以似乎連接到數據庫,但無法從數據庫中的註冊表中獲取任何數據。在此先感謝我一直在運行window7。連接php到mysql
<?php
$username = "root";
$password = "deslap";
$hostname = "localhost";
#connection to the database seems to work and prints connected to MySQL
$dbhandle = mysql_connect($hostname, $username, $password)or die("Unable to connect to MySQL");
echo "<br />Connected to MySQL<br>";
#select a database to work with
$selected = mysql_select_db('registered',$dbhandle)or die("Could not select database");
#execute the SQL query and return records.
$result = mysql_query("SELECT id, Name FROM registered");
while($row = mysql_fetch_array($result))
{
echo "ID:".$row{'id'}." Name:".$row{'name'}."Email: ".$row{'Email'};
}
?>
</body>
</html>
請不要在新代碼中使用'mysql_ *'函數](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 *他們不再維護,並[已正式棄用](https://wiki.php.net/rfc/mysql_deprecation)*。看到[紅色框](http://uk.php.net/manual/en/function.mysql-connect.php)?學習[準備的語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。 [這篇文章](http://php.net/manual/en/mysqlinfo.api.choosing.php)將幫助你決定哪些。 – 2014-09-26 14:10:22
在打開'<?php'標籤後立即向文件頂部添加錯誤報告 'error_reporting(E_ALL); ini_set('display_errors',1);'看看它是否產生任何東西。如果數據庫連接失敗,也將'或者死亡(mysql_error())'變爲'mysql_query()'和'死亡'('Could not connect:'。mysql_error())'並且得到真正的錯誤。 – 2014-09-26 14:12:22
尋找更近一點我不知道是否數據庫被命名爲註冊或表是....還是兩者?看起來DB是因爲你的選擇不會失敗。表名是否相同? – 2014-09-26 14:20:47