I am trying to fetch data from a table using mysqli_query. When i use the following commands it works ok:
$hostname = "********";
$username = "*******";
$password = "********";
$databaseName = "**************";
$dbConnected = mysqli_connect($hostname, $username, $password, $databaseName);
When i try to include file with the above codes include('../htconfig/dbConfig.php'); then i do NOT get any results:
..."0 results"
<?php
include('../htconfig/dbConfig.php');
$dbConnected = mysqli_connect($db['hostname'], $db['username'], $db['password'], $db['databaseName']);
if(!$dbConnected) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($dbConnected) . "\n". "<br>";
mysqli_set_charset($dbConnected, "utf8");
$tPerson_SQLselect = "SELECT ";
$tPerson_SQLselect .= "ID, Salutation, FirstName, LastName, CompanyID ";
$tPerson_SQLselect .= "FROM ";
$tPerson_SQLselect .= "tPerson ";
$result = mysqli_query($dbConnected, $tPerson_SQLselect);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Salutation: ".$row["Salutation"]. "-FirstName: ".$row["FirstName"]." ".$row["LastName"]." -CompanyID: ".$row["CompanyID"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($dbConnected);
?>
I can not find my error.. Please help !
dbConfig.php file:
<?php
$db = array(
'hostname' => '*****',
'username' => '*****',
'password' => '*****',
'database' => '*****',
);
?>
Your $dbConnected should be like this if you're still using the same variable names:
include('../htconfig/dbConfig.php');
$dbConnected = mysqli_connect($hostname, $username, $password, $databaseName);
If you want it to be $db['field'] then your ../htconfig/dbConfig.php should be like this:
$db = array('hostname' => 'xxxx',
'username' => 'xxxx',
'password' => 'xxxx',
'databaseName' => 'xxxx');
EDIT: Your array in dbConfig.php says 'database' but you used 'databaseName' in your $dbConnected mysqli_connect function?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With