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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.