Hello I’m doing some testing trying to figure something random out after others working on this got me thinking of something of my own random I needed. How do I get the returned id from this into $returnid? The echo is saying “Undefined variable: get” so it’s not reading the pulled id. Please let me know why its not grabbing the id, thanks a lot.
<?php
$query = mysql_query("SELECT MAX(id) FROM `tabletest`") OR die('Not getting last id: ' . mysql_error());
$returnid=$get['id'];?>
<?php
echo "(result) $returnid;"; ?>
<?php
$query = "
SELECT
MAX(id) AS max_id
FROM
`tabletest`
";
$result = mysql_query($query);
if (!$result) {
echo 'Cannot pull last id: ' . mysql_error());
}
$row = mysql_fetch_assoc($result);
$max_id = $row['max_id'];
echo "Last ID: $max_id";
?>
mysql_query doesn’t itself return the results, what it ouputs is the resource ID of the result set. You then use mysql_fetch_assoc() to fetch the result. If there is to be bore then one row in the result set then you do:
$the_results=array(); // Set up an empty array
while ($row = mysql_fetch_assoc($result)) {
$the_results[]=$row;
}
No not to worry lol, I’m not getting too extravagant just trying to figure out specifics of ‘max’ not looking for exact code, but you’re a database guru great going so maybe you know something I don’t. I’m not inserting anything at all, only trying to pull the table ids out.
Yes capturing the max id is all I want and putting it into a variable can you help?
SpaceP thank you~ Some things do take some extra steps. I’ve seen many, many, things set up from one thing passed to another without needing a function until only if a function is even needed. I’ve seen the id passed many times without a function so shouldn’t this be working?
So-
<?php
$query = mysql_query("SELECT MAX(id) FROM `tabletest`") OR die('Not getting last id: ' . mysql_error());
WHILE($returnid = mysql_fetch_array($query))
{$returnid=$get['id'];}?>
<?php echo "(result) $returnid;"; ?>
why am I still getting the error: “Undefined variable: get” instead of an id number, is some extra float term now needed also, hopefully not. Just trying to echo the isolated and catured id, thanks.
No I really wasn’t, do I need some fancy variable not a usual one, to capture an id? I’m not doing any post forms, if I need it anyway I’ll try that first thanks.
$_GET[‘id’] in all that the same caused “Undefined index: id” error
So this
<?php $query = mysql_query(“SELECT MAX(id) FROM tabletest”) OR die('Not getting last id: ’ . mysql_error()); WHILE($returnid = mysql_fetch_array($query)) {$returnid=$get[‘id’];}?>
<?php echo “(result) $returnid;”; ?>
or with $_GET[‘id’] still not grabbing the max id
Whoa ho this one worked I found was never expecting an outcome with something like that.
<?php
$MaxID = mysql_query(“SELECT MAX(id) FROM tabletest”) OR die('Not getting last id: ’ . mysql_error());
$MaxID = mysql_fetch_array($MaxID, MYSQL_BOTH);
$MaxID = $MaxID[0];
?>
<?php
echo “(result) $MaxID;”; ?>
If anyone knows how to do it with the ‘while’ outside a function also please let me know. : D Thanks for your help.