Jump to content

[SOLVED] This should be so very simple.....i think? Retrieving max numeric value.


gaffafoote

Recommended Posts

Hi All,

 

Relatively inexperienced php'er. I can't see anything wrong with this code at all. Trying to retreive the last(highest) id from the text_id column which is auto-increment and primary key.

 

The conncetion to the DB is fine and working. It appears that the result isn't being drawn though........any ideas??

 

<?php

 

require_once ('mysql_connect.php'); //Connect to DB

 

$query = "SELECT MAX (text_id) FROM text";

 

$result = mysql_query ($query); //run the query

echo $result;

$row = @mysql_fetch_row($result);

$tid = $row[0];

 

echo $tid;

 

mysql_close();

?>

 

 

 

Cheers,

 

Gareth.

For one if it does not work why are you supressing errors? Not a very good idea.

 

Second it is probably in your query statement, add the OR DIE(mysql_error()) to see.

<?php 

require_once ('mysql_connect.php'); //Connect to DB

$query = "SELECT MAX (text_id) FROM text"; 
  
$result = mysql_query ($query) OR DIE(mysql_error()); //run the query
echo $result;
$row = mysql_fetch_row($result); // remove the error supressor
$tid = $row[0];

echo $tid;

mysql_close();
?>

 

Error messages can be very useful sometimes.

Thanks to both of you.

I'm already falling into bad habits, must work on that.

 

It seems that the problem is with th SQL syntax.

 

If anybody who knows where i'm going wrong with this, i'd appreciate the help because this syntax has been taken directly from a couple of online sources.

 

Cheers.

This is the SQL error from the first post. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(text_id) FROM text' at line 1

 

After SANFLY's suggestion I have tried this code and got the message Resource ID#380:

 

$query = "SELECT text_id FROM text ORDER BY text_id DESC LIMIT 1";

$result = mysql_query ($query) OR DIE(mysql_error());

echo $result;

$tid = mysql_result($result, 0);

 

cheers.

So is there still a problem?

 

Try

<?
$query = "SELECT text_id FROM text ORDER BY text_id DESC LIMIT 1";
$result = mysql_query ($query) OR DIE(mysql_error());
$row = mysql_fetch_array($result);
echo $row['tid'];
?>

Just realised I was being a dumbass and both of these ways were working. It was the space after the MAX which was causing a problem and I don't know why SPANFLY's method didn't work first time but they're both working now. Peas and Love.

 

$query = "SELECT text_id FROM text ORDER BY text_id DESC LIMIT 1";

$result = mysql_query ($query) OR DIE(mysql_error());

$row = mysql_fetch_array($result);

echo $row[0];

 

 

/***************************/

 

$query = "SELECT MAX(text_id) FROM text";

 

$result = mysql_query ($query) OR DIE(mysql_error()); //run the query

$row = mysql_fetch_row($result);

 

echo $row[0];

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.