seb213 Posted May 27, 2009 Share Posted May 27, 2009 Hi All, I need some basic help with the syntax for fetching and printing tables from a MYSQL database. Easy right? well not for me lol Basically the table is 'users' and I need to print the row 'mail' where the row 'status' = 1 SELECT mail FROM users WHERE status='1' Im not sure how to echo this to a php page, please help? Quote Link to comment https://forums.phpfreaks.com/topic/159906-php-sql-query-help/ Share on other sites More sharing options...
Maq Posted May 27, 2009 Share Posted May 27, 2009 Please read the documentation and examples in the manual next time, mysql_query. $sql = "SELECT mail FROM users WHERE status='1'"; $result = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_assoc($result)) { echo $row['mail'] . " "; } Quote Link to comment https://forums.phpfreaks.com/topic/159906-php-sql-query-help/#findComment-843437 Share on other sites More sharing options...
seb213 Posted May 28, 2009 Author Share Posted May 28, 2009 @Maq Thank You thats perfect, and so simple. But perhaps if you have time you can explain the purpose of the line: while($row = mysql_fetch_assoc($result)) I dont quite understand what this does. But in any event, thanks again for a prompt reply Quote Link to comment https://forums.phpfreaks.com/topic/159906-php-sql-query-help/#findComment-843723 Share on other sites More sharing options...
gizmola Posted May 28, 2009 Share Posted May 28, 2009 seb213, When you query a relational database, you get a result set. It could have no rows, 1 row, or a million rows. The result set exists inside the database server. In order for the client who was querying the database to get rows from the database, you need to have the client perform a "Fetch". Each "fetch" will return one row. So the code you provided, basically calls a particular kind of "fetch" for mysql --- in this case a "fetch_assoc" that returns you an array in the form of an associative array. This in a while loop because the assumption is that you will want to keep fetching rows until all the rows in the result set have been returned. mysql_fetch_assoc() will return false when there are no further rows to fetch, which is why this code works. Quote Link to comment https://forums.phpfreaks.com/topic/159906-php-sql-query-help/#findComment-843728 Share on other sites More sharing options...
Maq Posted May 28, 2009 Share Posted May 28, 2009 @Maq Thank You thats perfect, and so simple. But perhaps if you have time you can explain the purpose of the line: while($row = mysql_fetch_assoc($result)) I dont quite understand what this does. But in any event, thanks again for a prompt reply Oh sorry. Gizmola provided you with a pretty solid explanation. In case you need further information you can look it up in the manual - mysql_fetch_assoc. Quote Link to comment https://forums.phpfreaks.com/topic/159906-php-sql-query-help/#findComment-843732 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.