2017-03-09 191 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_。 –