• 0

only echo first 6 lines


Question

here is my code

while ( $row = mysql_fetch_array ($result) ) {
$update = $row["update"];
echo ("- $update");
}

how can i make it so it only echos the first 6 values in the updates table

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

im thinking its something in here

$result = @mysql_query ("SELECT * FROM QuickUpdate");
if (!$result) {
echo("Please Contact Admin");
exit();
}

Link to comment
Share on other sites

  • 0

I'm a little rusty but you could try...

$result = @mysql_query ("SELECT * FROM QuickUpdate Desc Limit 6");
if (!$result) {
echo("Please Contact Admin");
exit();
}

I know there's another way but like I said I'm rusty. If interested I do know it involves counting the numbers of results and using a "for" statement rather than a "while".

Link to comment
Share on other sites

  • 0
$result = @mysql_query ("SELECT * FROM QuickUpdate LIMIT 0,5");

found it searched google for: mysql Desc Limit

that will work however it will echo the very first 6 rows, so if it is for updates it won't show the newest ones - you can either count the rows to do it or just add DESC at the end

"SELECT * FROM QuickUpdate LIMIT 0,5 DESC");

:)

Link to comment
Share on other sites

  • 0

i am not a mysql programmer or php programmer, but i do know other languages like C and i have always considered using exit as a sin in my programs. :)

its kinda like the blue screen of death ;)

Link to comment
Share on other sites

  • 0

keep in mind that exit() and die() (same function, different name) will stop the execution of the whole script and not just that loop/section/file

that is, even if you just use include() or require() it'll still stop the entire thing.

Link to comment
Share on other sites

  • 0

oh and if you wanted to do it without anything on the mysql-end (this is also known as "for future reference")

$res = mysql_query('SELECT * FROM my_table ORDER BY id DESC');

for ($i = 1;$i <= 6; $i++) {

$r = mysql_fetch_assoc($res);

echo $r['name'];

}

on the first line is the mysql query, the DESC means descending (like 3,2,1 - newest first)

second line, in the for() section the first expression $i = 1 creates the variable $i and assigns it the value 1, the second section $i <= 6 means do this while $i is less than or equal to 6, and the third section $i++ tells it what to do when it reaches the end of the code inside the { brackets } .. $i++ can be used anywhere, it increases the variable by 1. you can even do:

$i++;

as its own line.

http://www.php.net/manual/en/control-struc...uctures.for.php

php manual for the 'for' expression.

mysql_fetch_array is about half as efficient as mysql_fetch_assoc. if you're refering to columns only by their name ($r['category'] instead of $r[3]) always use mysql_fetch_assoc. if you want to know why, consult the PHP manual.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.