Jump to content

Return last row's data


TomMW

Recommended Posts

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>';

?>

Link to comment
https://forums.phpfreaks.com/topic/274903-return-last-rows-data/
Share on other sites

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)) {

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>';

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.