[英]PHP: Can't find my database
我在連接數據庫時遇到問題
<?php
@mysqli_connect( "127.0.0.1", "root", "root" ) or die( "Cannot connect to the database!" );
@mysqli_select_db( "phpherexamen" ) or die( "Connected to the database, but there's nothing here!" );
session_start();
?>
這也是我用來連接我的bd的代碼,但是我總是得到消息:
已連接到數據庫,但是這里什么都沒有!
我的數據庫看起來像這樣
我沒看到什么? :)
您應該將mysqli_connect函數的結果用作mysql_select_db函數的第二個參數。
$link = mysqli_connect( "127.0.0.1", "root", "root" )
if (!$link) {
die("Cannot connect to the database!");
}
$db_selected = mysql_select_db('phpherexamen', $link);
if (!$db_selected) {
die ("Connected to the database, but there's nothing here!");
}
使用過程樣式mysqli
您需要將mysqli_select_db
傳遞mysqli_select_db
mysqli
實例
$mysqli = mysqli_connect("127.0.0.1", "root", "root");
mysqli_select_db($mysqli, "phpherexamen");
將連接存儲在變量中,然后將其傳遞給select_db命令。
$db = mysqli_connect(...);
mysqli_select_db($db, 'phpherexamen');
如果您使用面向對象的樣式:
$handle = @mysqli_connect( "127.0.0.1", "root", "root" ) or die( "Cannot connect to the database!" );
$handle->mysqli_select_db( "phpherexamen" ) or die( "Connected to the database, but there's nothing here!" );
或程序樣式:
$handle = @mysqli_connect( "127.0.0.1", "root", "root" ) or die( "Cannot connect to the database!" );
@$mysqli_select_db($handle, "phpherexamen" ) or die( "Connected to the database, but there's nothing here!" );
但是Mysqli需要一個對象來知道mysql select db應該應用於哪個連接。
旁注:已被MySQLi取代的舊擴展MySQL支持緩存最后一個連接,因此您無需指定數據庫句柄,但是如果您的網站使用多個連接或要在循環時觸發查詢,則這是一個不錯的選擇結果或類似的結果,為什么它只能與有效的句柄或更好的所述對象一起使用,所以比其他舊方法更理智。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.