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($m开发者_Python百科axquery);
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";
}
?>
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";
}
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']
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.
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.
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.
The query is looking fine. Please inspect your code of getting data from the result set step by step.
精彩评论