2

I have a table and i want to echo the 2 last rows of my tabel, I used the below code but just the last one showed, what is the problem.

$result1 =(mysql_fetch_array(mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")));

Print $result1['time'];
  • 1. Don't use `mysql_` functions. They are deprecated. 2. You will have to continuously call this function in order to move the pointer and get the next row. – ಠ_ಠ Jul 25 '13 at 12:18
  • last 2 COLUMNS or last two ROWS??? – STT LCU Jul 25 '13 at 12:25
  • @zdhickman why souldn't i use mysql ??? – alexis Leone Jul 25 '13 at 12:41
  • @STTLCU you are right, last 2 rows – alexis Leone Jul 25 '13 at 12:41
  • 2
    @alexisLeone [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Prasanth Jul 25 '13 at 12:54
  • but when i use mysqli insted of sql it cuase error, what to do? – alexis Leone Jul 25 '13 at 12:58
  • You don't have to simply replace any "mysql_" you see with a "mysqli_", you need to change something here and there... – STT LCU Jul 25 '13 at 13:04
  • so could you tell me how to update my sql code to sqli ? – alexis Leone Jul 25 '13 at 13:07

6 Answers6

2

mysql_fetch_array = 1 fetch.

do it again for fetching 2nd result.

Also, use mysqli eh.

Prasanth
  • 5,230
  • 2
  • 29
  • 61
1

You're doing mysql_fetch_array only one time, so it gets the first element. If you want to get all the elements, then do it again, or use a loop.

Something like:

$query = "SELECT * FROM $table ORDER BY id DESC LIMIT 2";

while($row = mysql_fetch_array(mysql_query($query) )
{
    echo $row['time'].'<br>'; 
}
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

My solution:

$limit = 2;

$sql = "SELECT * FROM $table ORDER BY id DESC LIMIT $limit";
$result = mysql_query($sql);

$array = array(); $i = 0;
while($row = mysqli_fetch_array($result))
{
    $array[$i] = $row;
    $i++;
}

var_dump($array[0]);
var_dump($array[1]);
Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24
0

For 2 Or more rows you need to loop it

$sql = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")

while($row=mysql_fetch_array($sql))
{
    echo $row['time']."<br>";
}
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
0
$query = mysqli_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2");
while ($result = mysqli_fetch_array($query)) {
    echo $result['time'];
}

Gives out every return of your database (2 in this case). You should use mysqli_-functions.

de_nuit
  • 650
  • 7
  • 13
0

Maybe you should try like this, since mysql_fetch_array returns only one row

while ($row = mysql_fetch_array($yourQuery)) {
    echo $row["yourAlias"];
}

Further details here : http://fr2.php.net/manual/en/function.mysql-fetch-array.php

Yobac
  • 43
  • 8