php-显示mysql选择查询结果
我想用php和mysql带来一个select语句的结果 我尝试使用my代码来实现php-显示mysql选择查询结果,php,mysql,Php,Mysql,我想用php和mysql带来一个select语句的结果 我尝试使用my代码来实现 <head> <meta http-equiv = "content-Type" content = "text/html" charset = "utf-8"> </head> <?php $conn = mysqli_connect("hostname","user","password","dbname"); // Check connection if (mysql
<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);
?>
输入真正的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
这边来
为什么我的代码没有输出
我需要以任何方式进行更改吗?您正在使用的返回带有数字键的数组。您应该使用或mysqli\u fetch\u数组($result,mysqli\u 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++;
}
$no=1;
while($row=mysqli\u fetch\u assoc($result)){
回声“;
回显“$否”;
回显“$row['kind']”;
回显“$row['money']”;
回声“;
$no++;
}
这是mysqli\u fetch\u数组的手册
var_dump函数和print_r函数将非常有用
var_dump($result)或var_dump($row)无需更改代码中的任何内容,只需将
$row=mysqli_fetch_数组($result)
替换为$row=mysqli_fetch_数组($result,mysqli_ASSOC)
;你会没事的。改用mysqli\u fetch\u assoc
或传递mysqli\u assoc
标志。尽管更改。没有结果。你能在while循环中发布var_dump($row)的结果吗?如何使用var_dump($row)?这是什么?你应该在之后粘贴var\u dump($row),而($row=mysqli\u fetch\u assoc($result)){
。虽然发生了变化,但没有结果。检查你的sql是否构造正确。我是说只需回显它就可以了。这是php查询编码的问题。
$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++;
}