bobo
bobo

Reputation: 13

Problem with max SQL function

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";
    }
?>

Upvotes: 1

Views: 6250

Answers (6)

user319198
user319198

Reputation:

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

Upvotes: 0

m.edmondson
m.edmondson

Reputation: 30922

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.

Upvotes: 0

Pradeep Singh
Pradeep Singh

Reputation: 3634

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";
}

Upvotes: 6

mspir
mspir

Reputation: 1734

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.

Upvotes: 1

symcbean
symcbean

Reputation: 48387

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']

Upvotes: 1

Ho&#224;ng Long
Ho&#224;ng Long

Reputation: 10848

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.

Upvotes: 0

Related Questions