I’m trying to pass a variable from a dbh.inc.php
to index.php
using include_once
, But it gives me an error in $con
at index.php
that it is not defined
dbh.inc.php:
<?php $dbServername = "localhost"; $dbUsername = "root"; $dbPassword = ""; $dbName = "loginsystem"; $con = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
indux.php:
<?php include_once 'includes/dbh.inc.php'; ?> <!DOCTYPE html> <html lang=""> <head> <title></title> </head> <body> <?php $sql = 'SELECT * FROM users;'; $result = mysqli_query($con, $sql); $resultCheck = mysqli_num_rows($result); if($resultCheck > 0) { while ($row = mysqli_fetch_assoc($result)) { echo $row['user_uid'] . "<br>"; } } ?> </body> </html>
Advertisement
Answer
Usually you can access variables from one file to another by including file like:
vars.php
<?php $color = 'green'; $fruit = 'apple'; ?>
test.php
<?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?>
First of all check it by turning on the error reporting for that particular file and you will get error if the included path was not correct.
you can add it as follow:
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
Thanks