2017-03-09 259 views
0

爲什麼我無法連接數據庫?我已經下面的代碼一個文件「connect.inc.php」:無法連接數據庫

 <?php 
     $conn_error='Could not connect.'; 
     $mysql_host='localhost'; 
     $mysql_user='root'; 
     $mysql_pass=''; 
     $mysql_db='a_database'; 
     if([email protected]_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_db) 
     || [email protected]_select_db(mysqli_connect($mysql_host,$mysql_user 
     ,$mysql_pass,$mysql_db), $mysql_db)) 
     { 
     die($conn_error); 
     } 
     ?> 

,另一種是具有下面的代碼索引頁:

 <?php 
     require 'connect.inc.php'; 
     echo 'Ok'; 
     ?> 

這是錯誤:

  Warning: require(connect.inc.php): failed to open stream: No such 
     file or directory in C:\xampp\htdocs\ConnectingToServerAndDatabase 
     \index.php on line 2 


     Fatal error: require(): Failed opening required 'connect.inc 
     php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\ 
     ConnectingToServerAndDatabase\index.php on line 2 

Screen Shot of error.

+0

@選項OP使用'mysqli' ... – arkascha

+0

問題是包含該php文件,而不是連接到數據庫。 – arkascha

+0

我只使用mysqli_。 –

回答

0

Warning: require(connect.inc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\ConnectingToServerAndDatabase \index.php on line 2

Fatal error: require(): Failed opening required 'connect.inc php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\ ConnectingToServerAndDatabase\index.php on line 2

connect.inc.php文件應在同一目錄下index.php存在

您可以使用下面的命令來設置包括路徑

set_include_path(get_include_path().":"."/path/to/your/conn_file_directory"); 

# and then use 
require 'connect.inc.php' 

或使用require __DIR__ . "/relative/path/from/current/file"

mysql_ 功能現在已經過時,所以在使用mysqli_功能。

+0

我只用過mysqli_。 –