Use single line query with MySQL and PHP

say i have it

$result =  mysql_query('SELECT views FROM post ORDER BY views ASC');

and I want to use the value at index 30, I assumed that I would use

mysql_data_seek($result, 30);
$useableResult = mysql_fetch_row($result);
echo $useableResult . '<br/>';

But this returns the whole table

How am I wrong?

Edit: Woops, I actually have

mysql_data_seek($result, 30);
while($row = mysql_fetch_row($result)){
    echo $row['views'] . '<br/>';
}
+3
source share
3 answers

Just use the SQL WHERE clause.

$result = mysql_query('SELECT views FROM post WHERE ID=30')

If you do not want to go by ID, but instead want to get the 30th element to be returned, you can use LIMIT min, max:

$result = mysql_query('SELECT views FROM post LIMIT 30, 1')

In both cases, your ORDER BY commands become unnecessary.

Here is a good example of using the LIMIT command to swap large sets of records.

+5
source

, , . - , 30- .

, :

$result =  mysql_query('SELECT views FROM post ORDER BY views ASC LIMIT 30,1');

, LIMIT Soviut - (, ), (min, max).

+2

Are you sure there are 30 elements in $ result? You can check if there is 30> mysql_num_rows ().

0
source

All Articles