1

I'm using include connection file to connect to the database. My challenge is how do I fetch from the database this is where am stuck.

include 'connection.php';

$sql = 'SELECT * FROM country';
$results = mysqli_query($sql);
ManuelJE
  • 496
  • 6
  • 15
user3671937
  • 45
  • 2
  • 9

1 Answers1

4

assume your connection.php contain

  <?php
        $con = mysqli_connect("localhost","my_user","my_password","my_db");

        if (mysqli_connect_errno())
        {
              echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
  ?>

So the in your file, you're using include 'connection.php' to get the connection. By using include its act like single page now. Then you've to use it like below

  require_once 'connection.php';

  $sql= 'SELECT * FROM country';
  $results = mysqli_query($con, $sql); # add connection string to query 

Explanation

when you add this include 'connection.php'; then whatever the data on parent(connection.php) file (ex: variable, Functions, etc ..) will come to child.


Links to refer

  1. In PHP, how does include() exactly work?
  2. Are PHP include paths relative to the file or the calling code?
  3. include, include_once, require or require_once?
Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85