Rusty.Metal Posted March 14, 2003 Share Posted March 14, 2003 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 More sharing options...
0 sanctified Veteran Posted March 14, 2003 Veteran Share Posted March 14, 2003 with a cycle? I cant remember my programmer lessons Link to comment Share on other sites More sharing options...
0 Rusty.Metal Posted March 14, 2003 Author Share Posted March 14, 2003 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 More sharing options...
0 PiMPiN Posted March 14, 2003 Share Posted March 14, 2003 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 More sharing options...
0 Rusty.Metal Posted March 14, 2003 Author Share Posted March 14, 2003 $result = @mysql_query ("SELECT * FROM QuickUpdate LIMIT 0,5"); found it searched google for: mysql Desc Limit Link to comment Share on other sites More sharing options...
0 pip Posted March 14, 2003 Share Posted March 14, 2003 $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 More sharing options...
0 Rusty.Metal Posted March 14, 2003 Author Share Posted March 14, 2003 i added a id row ;) and desc Link to comment Share on other sites More sharing options...
0 mackol Posted March 15, 2003 Share Posted March 15, 2003 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 More sharing options...
0 ike Posted March 15, 2003 Share Posted March 15, 2003 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 More sharing options...
0 ike Posted March 15, 2003 Share Posted March 15, 2003 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 More sharing options...
Question
Rusty.Metal
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