Jump to content

PHP SQL query help


seb213

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/159906-php-sql-query-help/
Share on other sites

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'] . "
";
}

Link to comment
https://forums.phpfreaks.com/topic/159906-php-sql-query-help/#findComment-843437
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/159906-php-sql-query-help/#findComment-843728
Share on other sites

@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.

Link to comment
https://forums.phpfreaks.com/topic/159906-php-sql-query-help/#findComment-843732
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.