[英]How do you query a MySQL table columns highest entry and store it in a variable
How do you query a MySQL table columns highest entry and store it in a variable. 您如何查询MySQL表列的最高条目并将其存储在变量中。 Here is what I have but it is not working.
这是我所拥有的,但无法正常工作。
$query = "SELECT MAX(date) FROM Records WHERE ips='$currentip'";
$result = mysql_query($query) or die(mysql_error());
echo $result;
<?php
$db = mysql_connect("localhost", "123", "123");
mysql_select_db("123");
$currentip='123.456.789';
$query = "SELECT MAX(date) FROM Records WHERE ips='$currentip'";
$date = mysql_result($result, 0);
echo $date;
?>
Just to point out, when you try to echo $result
you are only echoing the resource since that is what mysql_query
returns. 只是要指出,当您尝试回显
$result
您只是在回显资源,因为这是mysql_query
返回的。
If you want to echo the column, try: 如果要回显该列,请尝试:
while($row = mysql_fetch_assoc($result)) {
echo $row['date'];
}
Or you could use mysql_result
which return the return value into a string. 或者,您可以使用
mysql_result
将返回值返回为字符串。
$query = "SELECT MAX(date) FROM Records WHERE ips='$currentip'";
$date = mysql_result($result, 0);
echo $date;
Either should work, it hasn't been tested or compiled. 无论哪种都可以,尚未经过测试或编译。
<?php
$db = mysql_connect("localhost", "123", "123");
mysql_select_db("123");
$currentip='123.456.789';
$query = "SELECT MAX(date) FROM Records WHERE ips='$currentip'";
$result = mysql_query($query); // <--you forgot this line.
$date = mysql_result($result, 0); // <--now $result has a valid resource.
echo $date;
?>
I would also strongly suggest that since you are a new PHP programmer, get into the habit of incorporating some form of error-handling/checking, such as 我也强烈建议您既然是新的PHP程序员,请养成合并某种形式的错误处理/检查的习惯,例如
$db = mysql_connect("localhost", "123", "123");
if (!$db) {
die('Could not connect: ' . mysql_error());
}
...
$result = mysql_query($query);
if (!$result) {
die('Could not perform query: ' . mysql_error());
}
Performing error-checks from the start of your programming experience is really good practice and will better suit you for the long-run. 从编程经验开始就进行错误检查是一种很好的做法,并且从长远来看将更适合您。
在查询中使用max()
select max(column_name) from table_name
Max()将与group by子句一起使用
"SELECT MAX(date) AS mdate FROM Records WHERE ips='$currentip' GROUP BY ips";
$myQuery = "SELECT MAX(date) FROM Records WHERE ips='$currentip'";
$result = mysql_query($myQuery,$connection);
$mydate= mysql_fetch_row($result);
echo $mydate[0];
You absolutely need to read the manual chapter for the MySQL Functions . 您绝对需要阅读MySQL函数的手册章节。 Composing a program by picking random functions from third-party code and trying to guess how they work can only lead to a nervous breakdown.
通过从第三方代码中选择随机函数并试图猜测它们的工作方式来组成程序,只会导致神经崩溃。
To sum up: 总结一下:
I particularly like mysql_fetch_assoc(): 我特别喜欢mysql_fetch_assoc():
<?php
# ...
if( $row = mysql_fetch_assoc($resource_id) ){
echo $row['MAX(date)'];
}
?>
I'd also recommend some tutorial about basic SQL. 我还建议一些有关基本SQL的教程。 For instance, you can define aliases to make results easier to handle:
例如,您可以定义别名以使结果更易于处理:
SELECT MAX(date) AS max_date FROM ...
echo $row['max_date'];
Last but not least, if you put your site online before reading about SQL Injection please let us know so we can hack it ;-) (Hint: http://es.php.net/manual/en/function.mysql-real-escape-string.php ) 最后但并非最不重要的一点是,如果您在阅读有关SQL注入的信息之前将您的网站放到网上,请告知我们,以便我们对其进行破解;-)(提示: http : //es.php.net/manual/zh/function.mysql-real -escape-string.php )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.