关于mysql:php mysqli_query结果没什么

php mysqli_query results nothing

本问题已经有最佳答案,请猛点这里访问。

我正在尝试使用mysqli_查询从表中获取数据。当我使用以下命令时,它工作正常:

1
2
3
4
5
$hostname ="********";
$username ="*******";
$password ="********";
$databaseName ="**************";
$dbConnected = mysqli_connect($hostname, $username, $password, $databaseName);

当我试图用上述代码包括文件时("../htconfig/dbconfig.php");那么我不会得到任何结果:

…"0个结果"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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) ."
"
."";
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"]."";
}
} else {
    echo"0 results";
}

mysqli_close($dbConnected);
?>

我找不到我的错误。请帮助!

dbconfig.php文件:

1
2
3
4
5
6
7
8
<?php
$db = array(
'hostname' => '*****',
'username' => '*****',
'password' => '*****',
'database' => '*****',
);
?>


如果仍然使用相同的变量名,则$dbconnected应该如下所示:

1
2
include('../htconfig/dbConfig.php');
$dbConnected = mysqli_connect($hostname, $username, $password, $databaseName);

如果您希望它是$db['field'],那么../htconfig/dbconfig.php应该如下所示:

1
2
3
4
$db = array('hostname' => 'xxxx',
            'username' => 'xxxx',
            'password' => 'xxxx',
            'databaseName' => 'xxxx');

编辑:dbconfig.php中的数组显示"database",但在$dbconnected mysqli_connect函数中使用了"databasename"?