A simple auto-sending mail system (III)
Here is the introduction of PHP and MySQL together practical. How to extract data from the MySQL database.
Well, we have successfully completed our requirements, a lot of data already exist in the database, now the question is how to query the data, to get useful results?
In the following program, we will select the "Apple" user output.
--------------------------------------------------------
/* Declare some required variables */
$hostname = "Yourhostname";
$username = "YourUserName";
$password = "YourPassword";
$userstable = "Information"; /* Access information using MySQL-built data tables */
$dbName = "Yourdbname";
/* Connect to Database */
Mysql_connect ($hostname, $username, $password) OR die ("Unable-CONNECT to database");
@mysql_select_db ("$dbName") or Die ("Unable to select database");
/* Select All "apple" users */
$query = "SELECT * from $userstable the WHERE (preference like ' Apples ')";
$result = mysql_query ($query);
/* Statistics on how many such users */
$number = Mysql_numrows ($result);
/* Output Results */
$i = 0;
IF ($number = = 0):
PRINT "
Nobody in the database prefers apples!
";
ELSEIF ($number > 0):
PRINT "
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 "
";
ENDIF;
?>
--------------------------------------------------------
Save him as a apples.php3.
Explanation: Some new functions are used:
1, $number = Mysql_numrows ($result);
Syntax: int mysql_num_rows (string result);
result the array record returned from the function mysql_query.
• Returns the number of rows that exist in the $result.
2, $name = Mysql_result ($result, $i, "name");
Syntax: int mysql_result (int result, int i, column);
This function separates the records and assigns each one to the variable.
• $result refers to the array result in.
• $i refers to the rows of the data.
column refers to the name of a column in a MySQL data table. You can also use variables.
So with a simple while loop, we can easily output the data to the browser.
http://www.bkjia.com/PHPjc/315719.html www.bkjia.com true http://www.bkjia.com/PHPjc/315719.html techarticle a simple auto-send mail system (iii) Here is the introduction of PHP and MySQL together practical. How to extract data from the MySQL database. Well, we have successfully completed our want ...