amycoll Posted April 3, 2010 Share Posted April 3, 2010 This is an script which recieves a news from database (MySQL). There are 4 news in database, but echo prints only one of them. (first). mysql_connect("localhost", "root", "pass"); mysql_select_db("commander"); $query = mysql_query("SELECT * FROM news"); $rows = mysql_fetch_array($query); echo "$rows[title]"; echo "$rows[description]"; Quote Link to comment https://forums.phpfreaks.com/topic/197452-php-echo-problem-only-one-row/ Share on other sites More sharing options...
Potatis Posted April 3, 2010 Share Posted April 3, 2010 while($row = mysql_fetch_array($query)) { echo $row['title']; echo $row['description']; } Is what you need. Quote Link to comment https://forums.phpfreaks.com/topic/197452-php-echo-problem-only-one-row/#findComment-1036382 Share on other sites More sharing options...
amycoll Posted April 3, 2010 Author Share Posted April 3, 2010 It echoes like 1000000 of them, Quote Link to comment https://forums.phpfreaks.com/topic/197452-php-echo-problem-only-one-row/#findComment-1036393 Share on other sites More sharing options...
ignace Posted April 3, 2010 Share Posted April 3, 2010 If you only use $row['world'] type use mysql_fetch_assoc if you want to do something like list($field1, $field2) then use mysql_fetch_row mysql_fetch_array returns both whereby you get: Array ( [0] => hello, ['world'] => hello, [1] => bar, ['foo'] => bar ) Quote Link to comment https://forums.phpfreaks.com/topic/197452-php-echo-problem-only-one-row/#findComment-1036396 Share on other sites More sharing options...
PFMaBiSmAd Posted April 3, 2010 Share Posted April 3, 2010 To return specific rows you are interested in, you must use a WHERE clause in your query - http://w3schools.com/php/php_mysql_where.asp Quote Link to comment https://forums.phpfreaks.com/topic/197452-php-echo-problem-only-one-row/#findComment-1036398 Share on other sites More sharing options...
Lukeidiot Posted April 3, 2010 Share Posted April 3, 2010 $query = mysql_query("SELECT * FROM news LIMIT 10"); while($row = mysql_fetch_array($query)) { echo $row['title']; echo $row['description']; } Is what you need. Quote Link to comment https://forums.phpfreaks.com/topic/197452-php-echo-problem-only-one-row/#findComment-1036457 Share on other sites More sharing options...
amycoll Posted April 3, 2010 Author Share Posted April 3, 2010 I don't want to echo specific ones, just normal ones but it gives only one announcement/new instead it gives 4 of them as theres 4 of them in DB. Quote Link to comment https://forums.phpfreaks.com/topic/197452-php-echo-problem-only-one-row/#findComment-1036475 Share on other sites More sharing options...
amycoll Posted April 4, 2010 Author Share Posted April 4, 2010 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/197452-php-echo-problem-only-one-row/#findComment-1036703 Share on other sites More sharing options...
Lee Posted April 4, 2010 Share Posted April 4, 2010 If there are 4 of them in the db, then as PFMaBiSmAd says, you need to use the WHERE clause in your query to specify which one to output. Otherwise, the loop will keep outputting every row in your sql table. Quote Link to comment https://forums.phpfreaks.com/topic/197452-php-echo-problem-only-one-row/#findComment-1036705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.