tags:

views:

51

answers:

6

How do you query a MySQL table columns highest entry and store it in a variable. 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;

Update:

<?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;
?>
A: 

use max() in your query

Galen
A: 
select max(column_name) from table_name
Salil
A: 

Max() will work with group by clause

"SELECT MAX(date) AS mdate FROM Records WHERE ips='$currentip' GROUP BY ips"; 
nik
how do you echo the date
see my answer below.
Anthony Forloney
use MAX(date) AS mdate, and then echo $fetchdata['mdate']
nik
+1  A: 

Just to point out, when you try to echo $result you are only echoing the resource since that is what mysql_query returns.

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.

$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.

In regards to your updated code:

<?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

$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.

Anthony Forloney
Thanks. Im climbing this cliff and it only gets higher. ps. new user.
Every new user experiences problems, at least you know a good resource to come search for answers, and if none are found, ask questions.
Anthony Forloney
mysql_result method still returns error.Wrong parameter count for mysql_result()
I had edited the answer, did you supply a row number, since it should be the first row, I used `0`. Also, have you opened up a connection to the database, using `mysql_connect`? If not, let me know and I'll update the answer with an example.
Anthony Forloney
i have not + there is too much going on right now. Hard to focus.
yep. connection is there, everything is right. Row 0 is the first row? IDK. I dont want bugs in this part of the application.
Yeah, rows start at index `0`. As long as you have a connection, you should be good to go.
Anthony Forloney
<?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;?>What is wrong here. It returns error mysql_result(): supplied argument is not a valid MySQL result resource
@user315819, inside `mysql_result` you need to supply a valid MySQL resource. Resources are given by calling `mysql_query` which you are not. See my above edit for what you need to do.
Anthony Forloney
A: 
 $myQuery = "SELECT MAX(date) FROM Records WHERE ips='$currentip'";

 $result = mysql_query($myQuery,$connection);

 $mydate= mysql_fetch_row($result);

 echo $mydate[0];
Ranhiru Cooray
A: 

You absolutely need to read the manual chapter for the MySQL Functions. 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:

  • mysql_query() executes a query and returns a resource ID
  • You use such resource ID to fetch rows with any of the mysql_fetch_*() functions

I particularly like mysql_fetch_assoc():

<?php
# ...
if( $row = mysql_fetch_assoc($resource_id) ){
    echo $row['MAX(date)'];
}
?>

I'd also recommend some tutorial about basic 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)

Álvaro G. Vicario