PDA

Click to See Complete Forum and Search --> : Mysql help


TetovaBoy
Jun 4th, 2010, 09:44 AM
Hi first of all i am a beginner in PHP and MYSQL
I need help on how can i get the list of the emails from user table

i want the emails to be listed on user_emails.php file

Can some one help me on how to do this..?

kows
Jun 4th, 2010, 08:34 PM
first, you would want to connect to MySQL and select the database you'll be using:

mysql_connect(host, username, password);
mysql_select_db(database);

then, you should construct an SQL query. they generally have a syntax similar to this:
SELECT field1[, field2[, field3[, ...]]]
FROM table
[WHERE expression]
[ORDER BY field1[, field2[, ...]] [ASC|DESC]]

anything within square brackets is optional. if you have a field named "email," then we can just select that. if we don't want to filter or sort the results, we can also ignore the WHERE and ORDER BY clauses:
SELECT email FROM table

you should store this query in a variable -- $sql, maybe. then, we can query the database:
$query = mysql_query($sql);

then, we can fetch the results in an associative array, one row at a time, in a loop until the query has no results left:
while($row = mysql_fetch_assoc($query)){

echo $row['email'] . "\n";

}

note that our SQL query selects one field -- email -- and we can print that email by referencing the "email" key inside of the $row array.

hope that gives you the general idea.