0

In code below I will be displaying posts in table. Now for creating load more function I need to get id of last post in table but I am not able to do that, Here $ID = $row['id']; gets me id of first post may be because it's outside loop and to get id of last post I must place this code inside loop. But where to exactly place it so that I get id of last post displayed in table.

<?php
$sql = "SELECT * FROM posts ORDER BY id desc limit 3";
$query = $db->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
$ID = $row['id'];
?>

<table>
<tr>
<?php do { //horizontal looper?>
<td>
<div><h2><?php echo $row['title']; ?></h2></div>          
<div><p><?php echo $row['body']; ?></p></div>
<img src='<?php echo $row['pic']; ?>'>
<div><p><?php echo $row['about']; ?></p></div>
</td>
<?php
$row = $query->fetch(PDO::FETCH_ASSOC);
if (!isset($nested_List)) {
$nested_List= 1;
}
if (isset($row) && is_array($row) && $nested_List++%3==0) {
echo "</tr><tr>";
}
} while ($row); //end horizontal looper 
?>
</table>
bɪˈɡɪnə
  • 1,087
  • 2
  • 23
  • 46
  • 1
    Simply use `$id = $db->lastInsertId();` after `$query->execute();` – Narendrasingh Sisodia Oct 31 '15 at 10:46
  • 1
    Possible duplicate of [PDO get the last ID inserted](http://stackoverflow.com/questions/10680943/pdo-get-the-last-id-inserted) – Narendrasingh Sisodia Oct 31 '15 at 10:47
  • @Uchiha I am not talking about database table – bɪˈɡɪnə Oct 31 '15 at 10:56
  • please read question again its not duplicate of what you have marked, I am displaying posts in a table(not db table) so i want to catch id of post that is displayed at last in table – bɪˈɡɪnə Oct 31 '15 at 10:59
  • hello, you need only last entry of post in database table right? – Darshan Dave Oct 31 '15 at 11:02
  • @darshandave not db table ,,, I am displaying posts in table, i want to get id of that last post displayed in table – bɪˈɡɪnə Oct 31 '15 at 11:06
  • Your "//horizontal looper" with that `$nested_List++%3==0`-thingy seems overly complicated (esp. given the "LIMIT 3" clause). You just want to display up to three items hoizontally next to each other and if there are more items in the database have a "next"-link? – VolkerK Oct 31 '15 at 14:30
  • @VolkerK yes horizontal loop works well three posts are displayed horizontly(like columns) but I can't catch id of last post displayed – bɪˈɡɪnə Nov 01 '15 at 13:00

2 Answers2

1

Try this code :-

    $ID1 = $row[0]['id'];//first id of table
    $ID2 = $row[1]['id'];//2 id of table
    $ID3 = $row[2]['id'];//3 id of table

get latest id:-

$sql = "SELECT * FROM posts ORDER BY id desc limit 1";
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
0

Try This Query

<?php 
 try { 
 $dbh = new PDO('mysql:host=localhost;dbname=test', 'username', 'password'); 

 $stmt = $dbh->prepare("INSERT INTO test (name, email) VALUES(?,?)"); 

  try { 
    $dbh->beginTransaction(); 
    $tmt->execute( array('user', 'user@example.com')); 
    $dbh->commit(); 
    print $dbh->lastInsertId(); 
  } catch(PDOExecption $e) { 
    $dbh->rollback(); 
    print "Error!: " . $e->getMessage() . "</br>"; 
} 
 } catch( PDOExecption $e ) { 
   print "Error!: " . $e->getMessage() . "</br>"; 
 } 
?> 
Darshan Dave
  • 645
  • 2
  • 9
  • 32