sanchez77 Posted November 10, 2010 Share Posted November 10, 2010 alright, soI want to query a table and grab the last record in a desc order and assign it plus one. My code so far for testing is: <?php ini_set('display_errors',1); error_reporting(E_ALL|E_STRICT); include "connect.php"; $result = mysql_query("SELECT * FROM tickets ORDER BY id DESC LIMIT 1"); echo "<table cellpadding='4' cellspacing='4'><tr> <th>id</th></br> </tr>";while($row = mysql_fetch_array($result)) { echo "<tr><td>" . $row['id'] . "</td></tr>"; } echo "</table>"; ?> So I can display the returned value, but how can I assign the returned value to a php variable? Quote Link to comment https://forums.phpfreaks.com/topic/218262-assign-a-php-value/ Share on other sites More sharing options...
.josh Posted November 10, 2010 Share Posted November 10, 2010 $id = $row['id']; or... just use $row['id'] as your variable since you only have 1 returned... Quote Link to comment https://forums.phpfreaks.com/topic/218262-assign-a-php-value/#findComment-1132542 Share on other sites More sharing options...
sanchez77 Posted November 10, 2010 Author Share Posted November 10, 2010 That's what I thought, but I can't get the value to display, is there something missing from the code? I can display the value within the table, but when I call the value in a simple echo statement, the value is blank. the little things that drive you batty. <?php ini_set('display_errors',1); error_reporting(E_ALL|E_STRICT); include "connect.php"; $result = mysql_query("SELECT * FROM tickets ORDER BY id DESC LIMIT 1"); echo "<table cellpadding='4' cellspacing='4'><tr> <th>id</th></br> </tr>";while($row = mysql_fetch_array($result)) { echo "<tr><td>" . $row['id'] . "</td></tr>"; } echo "</table>"; ticketnum= $row['id']; echo "hello" . ticketnum . "!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/218262-assign-a-php-value/#findComment-1132622 Share on other sites More sharing options...
ManiacDan Posted November 10, 2010 Share Posted November 10, 2010 After your WHILE loop finishes, $row will be false and will not have the 'id' property. You will have to fetch the variable INSIDE the loop. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/218262-assign-a-php-value/#findComment-1132628 Share on other sites More sharing options...
sanchez77 Posted November 10, 2010 Author Share Posted November 10, 2010 well that makes sense. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/218262-assign-a-php-value/#findComment-1132695 Share on other sites More sharing options...
DavidAM Posted November 10, 2010 Share Posted November 10, 2010 Note: Since you are limiting the SELECT to one row, you don't really need to use the WHILE loop: <?php ini_set('display_errors',1); error_reporting(E_ALL|E_STRICT); include "connect.php"; $result = mysql_query("SELECT * FROM tickets ORDER BY id DESC LIMIT 1"); echo "<table cellpadding='4' cellspacing='4'><tr> <th>id</th></br> </tr>"; $row = mysql_fetch_array($result); echo "<tr><td>" . $row['id'] . "</td></tr>"; echo "</table>"; $ticketnum= $row['id']; echo "hello" . $ticketnum . "!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/218262-assign-a-php-value/#findComment-1132777 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.