max SQL 函数的问题

发布于 2024-10-04 17:51:13 字数 1403 浏览 1 评论 0原文

我在 PHP 中有这段代码

$max="SELECT MAX(num) FROM info";
$maxquery= mysql_query($max) or die (died);
while($row = mysql_fetch_array($maxquery)){
    echo "The max num is ". $row['num']."this is it";
}
$maxnum= mysql_fetch_array($maxquery);
echo "<br>".$maxnum."hh";

,然后输出是:

The max num is this is it

hh


为什么查询没有得到最大数字?

该表称为 info,它具有以下字段:ID、num、标题、描述和答案。


编辑后:

我在 MySQL 中尝试了查询,效果很好!

“从信息中选择 MAX(num)”

,这是我的完整代码(如果有帮助的话):

<?php
    $answer=$_GET["answerbox"];
    $ID=$_GET["TheID"];

    $host="localhost";
    $username="root";
    $password="";
    $db_name="game";
    mysql_connect("$host","$username","$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $max="SELECT MAX(num) FROM info";
    $maxquery= mysql_query($max) or die (died);
    while($row = mysql_fetch_array($maxquery)) {
        echo "The max num is ". $row['num']."this is it";
    }

    $maxnum= mysql_fetch_array($maxquery);
    $sql="SELECT * FROM info  WHERE ID=".$ID;
    $query = mysql_query($sql) or die(errorquery);
    $row = mysql_fetch_array($query);
    $trueanswer = $row['Answer'];
    $num=$row['num'];
    if ($num<$maxnum)
    {
        $numto= $num +1 ;
        echo "<br>".$maxnum."hh";
    }
?>

I have this code in PHP

$max="SELECT MAX(num) FROM info";
$maxquery= mysql_query($max) or die (died);
while($row = mysql_fetch_array($maxquery)){
    echo "The max num is ". $row['num']."this is it";
}
$maxnum= mysql_fetch_array($maxquery);
echo "<br>".$maxnum."hh";

then the output be:

The max num is this is it

hh


Why didn't the query get the max number?

The table is called info and it has these fields, ID, num, title, description, and answer.


After editing:

I tried my query in MySQL and it works fine!

"SELECT MAX(num) FROM info"

and this is my complete code if it can help:

<?php
    $answer=$_GET["answerbox"];
    $ID=$_GET["TheID"];

    $host="localhost";
    $username="root";
    $password="";
    $db_name="game";
    mysql_connect("$host","$username","$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $max="SELECT MAX(num) FROM info";
    $maxquery= mysql_query($max) or die (died);
    while($row = mysql_fetch_array($maxquery)) {
        echo "The max num is ". $row['num']."this is it";
    }

    $maxnum= mysql_fetch_array($maxquery);
    $sql="SELECT * FROM info  WHERE ID=".$ID;
    $query = mysql_query($sql) or die(errorquery);
    $row = mysql_fetch_array($query);
    $trueanswer = $row['Answer'];
    $num=$row['num'];
    if ($num<$maxnum)
    {
        $numto= $num +1 ;
        echo "<br>".$maxnum."hh";
    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

半窗疏影 2024-10-11 17:51:13

那么代码将是

$max="SELECT MAX(num) as num FROM info";
$maxquery= mysql_query($max) or die (died);
while($row = mysql_fetch_assoc($maxquery)) {
    echo "The max num is ". $row['num']."this is it";
}

Then the code will be

$max="SELECT MAX(num) as num FROM info";
$maxquery= mysql_query($max) or die (died);
while($row = mysql_fetch_assoc($maxquery)) {
    echo "The max num is ". $row['num']."this is it";
}
甜宝宝 2024-10-11 17:51:13

您在结果集中查找的列不称为 num。尝试 print_r($row);查看数组索引是什么,或者给它一个别名,例如

$max="SELECT MAX(num) AS max_num FROM info";
...
   echo "The max num is ". $row['max_num']

The column you are looking for in the result set is NOT called num. Try print_r($row); to see what the array indices are, or give it an alias, e.g.

$max="SELECT MAX(num) AS max_num FROM info";
...
   echo "The max num is ". $row['max_num']
笑看君怀她人 2024-10-11 17:51:13

这是另一种更简单的方法:

$query="SELECT MAX(num) FROM info";
list ($max) = mysql_fetch_row(mysql_query($query));
print ($max);

数据库中的 num 字段必须是数字数据类型(即 int、float 等)才能正常工作。

Here is another and simpler way:

$query="SELECT MAX(num) FROM info";
list ($max) = mysql_fetch_row(mysql_query($query));
print ($max);

num field in your database has to be a numeric datatype (i.e. int, float etc) in order for this to work properly.

假扮的天使 2024-10-11 17:51:13

这是因为您的查询返回标量值,而不是数组。 $maxquery 是你想要获取的值。

我建议在尝试使用 PHP 运行查询之前检查数据库的查询。

It's because your query returns a scalar value, not an array. $maxquery is the value you want to get.

I recommend checking your query against the database before try running it with PHP.

小鸟爱天空丶 2024-10-11 17:51:13

在 MySQL 查询窗口中运行查询,然后分析输出。这样您就可以专注于查询,而不会受到 PHP 的干扰。

完成后修改问题,以便我们可以看到实际正在运行的查询。

Run your query within the MySQL query window then analyse the output. That way you can concentrate on the query without the PHP getting in the way.

Once you've done that modify the question so that we can see the query that's actually being run.

别忘他 2024-10-11 17:51:13

查询看起来很好。请逐步检查您从结果集中获取数据的代码。

The query is looking fine. Please inspect your code of getting data from the result set step by step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文