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);
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);
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