11

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' => '*****',
); 
?>
6
  • Do you get any errors? Commented Aug 3, 2015 at 17:41
  • Try require instead of include - are you certain that your file is being included correctly? Commented Aug 3, 2015 at 17:42
  • i dont see anything in my browser Commented Aug 3, 2015 at 17:42
  • yes it is included correctly because i tried echo the variables from dbConfig.php file Commented Aug 3, 2015 at 17:42
  • You're not error checking the query, you're assuming it will work as written. Commented Aug 3, 2015 at 17:43

1 Answer 1

9

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?

Sign up to request clarification or add additional context in comments.

7 Comments

i posted my dbConfig.php file at the end of the post
@N.K. check my update.
i did the check and.... Query failed
@N.K. Your array in dbConfig.php says 'database' but you used 'databaseName' in your $dbConnected mysqli_connect function?
You saved my day !! Thank you very much!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.