I am rather new to PHP--taught myself. But I am having a problem with the max fuction in a query. This is part of the program where it is going to look into a database to find the entry with the highest id number which is an auto-increment, therefore finding the newest entry. But whenever I do this, it never works correctly. I tried echoing the statement to see what the system came up with, but it always ends up with something like "Resource id #3" rather than just "3" which is what I need. What should I do to fix this? I'm sure there is a simple solution, but I have been working on this problem for at least 4 hours. X_X
$query23="select max(tut_id) from $tut where tut_approved='y'";
$id = mysql_query($query23)
or die ("too many users-- try again later ");
echo"$id";
$query="select * from $tut where tut_id='$id'";
$result = mysql_query($query)
or die ("too many users-- try again later ");
Here's the page-- http://www.airwalking.com/!omega/
Problems with max($value)
Moderator: General Moderators
- Seth_[php.pl]
- Forum Commoner
- Posts: 30
- Joined: Sun Aug 10, 2003 5:25 am
- Location: Warsaw / Poland
Use this code:
to see what is in your mysql_query() output.
Maybe try: select max(tut_id), * from $tut where tut_approved='y'
than use mysql_fetch_row to get data
Code: Select all
// $result is a mysql_query() output
while ( $row = mysql_fetch_row( $result ) )
{
foreach ( $row as $i => $value)
{
$column = mysql_field_name( $result, $i );
print $column.'='.$value.'<br>';
}
}Maybe try: select max(tut_id), * from $tut where tut_approved='y'
than use mysql_fetch_row to get data
-
airwalkery2k
- Forum Newbie
- Posts: 2
- Joined: Sun Aug 10, 2003 12:03 pm
I got max(tut_id)=3 when I ran that little program you put down. ...I am going to mess around with it... *messes around*
Yay! I got it to work. I just had to get rid of that print statement, and replace my $id with $value. Thanks Seth. (BTW, your mysql query ran as an invalid syntax...) I didn't take the time to see what the program really does, but here's the part now:
$query23="select max(tut_id) from $tut where tut_approved='y'";
$id = mysql_query($query23)
or die ("too many users-- try again later 1");
// $result is a mysql_query() output
while ( $row = mysql_fetch_row( $id ) )
{
foreach ( $row as $i => $value)
{
$column = mysql_field_name( $id, $i );
echo "$value";
$query="select * from $tut where tut_id='$value'";
$result = mysql_query($query)
or die ("too many users-- try again later 2");
}
}
Yay! I got it to work. I just had to get rid of that print statement, and replace my $id with $value. Thanks Seth. (BTW, your mysql query ran as an invalid syntax...) I didn't take the time to see what the program really does, but here's the part now:
$query23="select max(tut_id) from $tut where tut_approved='y'";
$id = mysql_query($query23)
or die ("too many users-- try again later 1");
// $result is a mysql_query() output
while ( $row = mysql_fetch_row( $id ) )
{
foreach ( $row as $i => $value)
{
$column = mysql_field_name( $id, $i );
echo "$value";
$query="select * from $tut where tut_id='$value'";
$result = mysql_query($query)
or die ("too many users-- try again later 2");
}
}