A simple automatic email sending system (3)
Here we introduce the practical combination of php and mysql. How to extract data from mysql database.
Okay, we have successfully completed our requirements. A lot of data already exists in the database. The question now is, how to query this data and get useful results?
In the following program, we will select the user output of "apple".
-------------------------------------------------- ------------
/* Declare some necessary variables*/
$hostname = "yourhostname ";
$username = "yourusername";
$password = "yourpassword";
$userstable = "information"; /* Use the data table created by MySQL to access information */
$dbName = "yourdbname";
/* Connect to database*/
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");
@mysql_select_db( "$dbName") or die( "Unable to select database");
/* Select all "apple" users*/
$query = "SELECT * FROM $userstable WHERE (preference LIKE 'Apples') ";
$result = MYSQL_QUERY($query);
/* Count how many such users there are*/
$number = MYSQL_NUMROWS($result);
/* Output result*/
$i = 0;
IF ($number == 0) :
PRINT "
Nobody in the database prefers Apples!
Users preferring Apples: $number
";
WHILE ($i < $number):
$name = mysql_result($result,$i,"name");
$email = mysql_result($result,$i,"email");
PRINT "Visitor $name likes Apples.
PRINT "Email address: $email.";
PRINT "
";
$i++;
ENDWHILE;
PRINT "