PHP 脚本 (mysqli) 不回显什么? [英] PHP script (mysqli) doesn't echo's something?
本文介绍了PHP 脚本 (mysqli) 不回显什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这段代码什么都不做?他没有回应结果.此脚本的目标是回显列的平均值(小数点后 2 位).
Why is this code doing nothing? He isn't echoing the result out.. The goal of this script is to echo the average of a column (on 2 decimals).
<?php
if (isset($_GET["age"]));
$age = ($_GET["age"]);
include($_SERVER["DOCUMENT_ROOT"] . "/3/includes/config.php");
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT AVG(price) FROM data WHERE age= '$age'") or die("Error: " . mysqli_error($con));
while($row = mysqli_fetch_array($result));
echo $row['price'];
die();
?>
推荐答案
您已经有效地将 echo
语句放在循环之后:
You have effectively put your echo
statement after the loop:
while($row = mysqli_fetch_array($result));
^ this is the problem
echo $row['price'];
... 等同于:
while($row = mysqli_fetch_array($result)) {}
echo $row['price'];
在循环之后 $row
将是 false
所以什么都不会被回显出来.
And after the loop $row
will be false
so nothing will be echoed out.
你想要:
while($row = mysqli_fetch_array($result))
echo $row['price'];
或者(更好,因为它会避免这些类型的错误......):
or (better as it will avoid these kinds of mistakes...):
while($row = mysqli_fetch_array($result)) {
echo $row['price'];
}
并且您应该使用准备好的语句来避免您现在遇到的 sql 注入问题.
And you should be using a prepared statement to avoid the sql injection problem you have now.
这篇关于PHP 脚本 (mysqli) 不回显什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文