php - 显示mysql选择查询结果

时间:2014-08-18 07:57:59

标签: php mysql

我正在使用php和mysql想要带来一个select语句的结果。

我尝试将我的代码用于实现

<head>
<meta http-equiv = "content-Type" content = "text/html" charset = "utf-8">
</head>
<?php
$conn = mysqli_connect("hostname","user","password","dbname"); // Check connection
if (mysqli_connect_errno()){
echo "MySQL error : " . mysqli_connect_error();
}
$result = mysqli_query($conn,"SELECT kind,ABS(SUM(money)) AS money FROM Account WHERE userid = 't@t.t' AND DATE_FORMAT(adate,'%Y/%c') ='2014/8'  AND checkio  = 'out'  GROUP BY kind;");
echo "<table border='1'>
<tr>
<th>No</th>
<th>kind</th>
<th>money</th>
</tr>";
$no = 1; 
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $no . "</td>";
echo "<td>" . $row['kind'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "</tr>";
$no++; 
}
echo "</table>";
mysqli_close($conn);
?>

没有数据没有结果。 喜欢这个

No  kind  money

输入真正的mysql Select语句

SELECT kind,ABS(SUM(money)) AS money FROM Account WHERE userid = 't@t.t' AND   DATE_FORMAT(adate,'%Y/%c') ='2014/8'  AND checkio  = 'out'  GROUP BY kind;

结果

kind money 
 A   1000
 B   2000
 C   1500

这样来......

为什么没有我的代码输出?

我需要以任何方式改变吗?

3 个答案:

答案 0 :(得分:0)

您正在使用mysqli_fetch_array()返回带有数字键的数组。您应该使用mysqli_fetch_assoc()mysqli_fetch_array($result,MYSQLI_ASSOC)

您的代码的修订版:

$no = 1; 
while($row = mysqli_fetch_assoc($result)){
    echo "<tr>";
    echo "<td>" . $no . "</td>";
    echo "<td>" . $row['kind'] . "</td>";
    echo "<td>" . $row['money'] . "</td>";
    echo "</tr>";
    $no++; 
}

答案 1 :(得分:0)

这是mysqli_fetch_array的手册 http://php.net/manual/en/mysqli-result.fetch-array.php

var_dump函数和print_r函数将很有用

var_dump($ result)或var_dump($ row)

答案 2 :(得分:0)

无需更改代码中的任何内容,只需将$row = mysqli_fetch_array($result)替换为$row = mysqli_fetch_array($result, MYSQLI_ASSOC);你没事。