0

Why I could not able to connect with database ? I have one file 'connect.inc.php' Which have following code:

     <?php
        $conn_error='Could not connect.';
        $mysql_host='localhost';
        $mysql_user='root';
        $mysql_pass='';
        $mysql_db='a_database';
        if(!@mysqli_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_db)
         || !@mysqli_select_db(mysqli_connect($mysql_host,$mysql_user
         ,$mysql_pass,$mysql_db), $mysql_db))
         {
         die($conn_error);
         }
        ?>

And another is index page Which have following code:

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

And this is the error:

         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.

  • @Option The OP does use `mysqli`... – arkascha Mar 09 '17 at 11:13
  • The issue is the inclusion of that php file, not the connection to the database. – arkascha Mar 09 '17 at 11:14
  • I had use mysqli_ only. – Rajeev_ranjan Mar 09 '17 at 11:14
  • as per your code your connect.inc.php file should be in same dir where index file located, confirm that – Omi Mar 09 '17 at 11:15
  • The connection code itself makes no sense at all: first remove those `@` characters, second you need to store the connection object you receive back from `mysqli_connect()`, otherwise you won't be able to use the connection afterwards and third one connection attempt is enough, no need to repeat it. – arkascha Mar 09 '17 at 11:16
  • both are in same directory – Rajeev_ranjan Mar 09 '17 at 11:18
  • Can you please check your file name is exactly like " connect.inc.php " ? – Muthu17 Mar 09 '17 at 11:19
  • @Option You are in very good company. This question is a very typical example of people commenting and answering faster than they can actually read the question, more concerned with contributing _anything_ instead of at least taking the time to understand the question. Very sad. – arkascha Mar 09 '17 at 11:36

1 Answers1

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

Your connect.inc.php file should be in same directory where index.php exists

You may use below command to set include path

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

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

OR use require __DIR__ . "/relative/path/from/current/file"

mysql_ functions are now deprecated, so use mysqli_ functions.

Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36