Last string/entry in array from SQL
Posted: Sun Sep 04, 2005 7:10 pm
Code: Select all
<?php
// Connecting, selecting database
$link = mysql_connect('localhost', 'NSF', 'klownz')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('blog_') or die('Could not select database');
// Performing SQL query
$query = "SELECT post_id FROM posts";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
$line = mysql_fetch_array($result, MYSQL_ASSOC);
print '<br><br>';
print end($line);
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>I just started learning php a week ago, i understand what most things do, but i can't really comprend everything yet, and it takes me a while to work it out.
Right now im trying to make a couple simple Forms, that will save the info in my "mysql" database, i have the forms, like the subject, and main entry working, but now im trying to get a working ID that will update for each entry, so if it is my first post it would be 1, second 2, etc. I figured i would look for a way to look for the last added number was in mysql, and when i retreve the info, i get it in array form. and i found the end() command to get the last entry in the array, but it isn't working correctly, i get the first entry, and i tryed reversing the array too, but still no luck, but i figured there would be a better way.
Right that php code outputs..
Code: Select all
Connected successfully
1Code: Select all
<?php
// Connecting, selecting database
$link = mysql_connect('localhost', 'NSF', 'klownz')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('blog_') or die('Could not select database');
// Performing SQL query
$query = "SELECT post_id FROM posts";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
print '<br><br>';
print_r ($line);
}
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
</body>
</html>Code: Select all
Connected successfully
Array ( [post_id] => 1 )
Array ( [post_id] => 5 )Basically what im trying to do, is what they do for php, is for each entry yuo have it gives it a post #, and then later you can call that post by the number.So im trying to get the Last added thing in mysql for "post_id" from table "posts".
-NSF