TomMW Posted February 25, 2013 Share Posted February 25, 2013 I'm stuck on what I believe to be a simple thing. What I want to do is return the DatePosted field from the last row in the table blog. This is what I have, which does give me the total number of posts or comments in the table, but it doesn't display anything for the Last Post. <?php $con = mysql_connect($database_host,$username,$password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($database_name, $con); $result = mysql_query("SELECT * FROM blog"); $blogcomments=mysql_num_rows($result); $sql = "SELECT * FROM blog WHERE CommentNo =" . $blogcomments .")"; $result = mysql_query($sql); echo '<p>Total: '. $blogcomments .'</p>'; echo '<p>Last Post: ' . $result['DatePosted'].'</p>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/ Share on other sites More sharing options...
Jessica Posted February 25, 2013 Share Posted February 25, 2013 You missed a step. mysql_fetch_assoc Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1414763 Share on other sites More sharing options...
TomMW Posted February 25, 2013 Author Share Posted February 25, 2013 Not sure I understand where or how to use the mysql_fetch_assoc() for this? Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1414764 Share on other sites More sharing options...
Jessica Posted February 25, 2013 Share Posted February 25, 2013 Too bad no one linked directly to the manual which has actual examples. Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1414765 Share on other sites More sharing options...
TomMW Posted February 25, 2013 Author Share Posted February 25, 2013 I looked at the sample code the first time around. And actually revised my script to: <?php $con = mysql_connect($database_host,$username,$password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($database_name, $con); $result = mysql_query("SELECT * From blog"); $blogcomments=mysql_num_rows($result); $sql = '"SELECT * FROM blog WHERE CommentNo="'. $blogcomments. '"'; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { echo $row["DatePosted"]; } echo '<p>Last Post: ' . $row['DatePosted'].'</p>'; echo '<p>Total: '. $blogcomments .'</p>'; ?> But now I get Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Websites\CitrusTwistKits\blog.php on line 258, which is while ($row = mysql_fetch_assoc($result)) { Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1414766 Share on other sites More sharing options...
Jessica Posted February 25, 2013 Share Posted February 25, 2013 Now time to read the link in my signature on debugging SQL Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1414767 Share on other sites More sharing options...
mweldan Posted February 25, 2013 Share Posted February 25, 2013 last row order by ascending or descending? // why not try something like this? select DatePosted from blog order by id desc limit 1; Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1414768 Share on other sites More sharing options...
TomMW Posted February 25, 2013 Author Share Posted February 25, 2013 Revised code again. Ugly I know but this time it works as I need it to: <?php $con = mysql_connect($database_host,$username,$password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($database_name, $con); $result = mysql_query("SELECT * From blog"); $blogcomments=mysql_num_rows($result); $result = mysql_query("SELECT * FROM blog"); while($row = mysql_fetch_array($result)) { $post = $row['DatePosted']; } echo '<p>Last Post: ' . $post .'</p>'; echo '<p>Total: '. $blogcomments .'</p>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1414769 Share on other sites More sharing options...
Jessica Posted February 25, 2013 Share Posted February 25, 2013 (edited) If all you need is the very last post you really should LIMIT it in the query. You also should do a COUNT in your other query to just get the number of rows. Edited February 25, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1414812 Share on other sites More sharing options...
Jessica Posted February 25, 2013 Share Posted February 25, 2013 If all you need is the very last post you really should LIMIT it in the query. YUo Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1414813 Share on other sites More sharing options...
Jessica Posted February 25, 2013 Share Posted February 25, 2013 If all you need is the very last post you really should LIMIT it in the query. YUo Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1414814 Share on other sites More sharing options...
P5system Posted February 26, 2013 Share Posted February 26, 2013 Your query should be like SELECT * From blog order by postedDate desc limit 1 Quote Link to comment https://forums.phpfreaks.com/topic/274903-return-last-rows-data/#findComment-1415082 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.