Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php mysqli_query results nothing [duplicate]

Tags:

include

php

mysql

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' => '*****',
); 
?>
like image 236
N.K. Avatar asked Aug 03 '15 17:08

N.K.


1 Answers

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?

like image 87
Mohamed Ebrahim Avatar answered Sep 21 '22 03:09

Mohamed Ebrahim