Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.