A simple automatic mail delivery system (III)

Source: Internet
Author: User
Tags mail connect mysql variables php and php and mysql mysql database
A simple automatic mail delivery system (III)

Here is an introduction to PHP and MySQL combined practical. How to extract data from the MySQL database.

Well, we have successfully completed our request, a lot of data already exist in the database, now the question is, how to query the data, get useful results?

In the following program, we will select the "Apple" user output.

--------------------------------------------------------

?


/* Declare some necessary variables * *

$hostname = "Yourhostname";
$username = "YourUserName";
$password = "YourPassword";
$userstable = "Information"; /* Use MySQL to create the data table 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);

* * Statistics How many of these users * *

$number = Mysql_numrows ($result);

/* Output Result * *

$i = 0;

IF ($number = = 0):

PRINT "<center><p>nobody in the database prefers apples!</center>";

ELSEIF ($number > 0):

PRINT "<center><p>users preferring Apples: $number <BR><BR>";

while ($i < $number):

$name = mysql_result ($result, $i, "name");
$email = mysql_result ($result, $i, "email");

PRINT "Visitor $name likes apples.<br>";
PRINT "Email Address: $email.";
PRINT "<BR><BR>";

$i + +;

Endwhile;

PRINT "</CENTER>";

ENDIF;


?>

--------------------------------------------------------

To save him as a apples.php3.

Explanatory notes: Some of the new functions 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 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 results in.
• $i refers to the rows of data.
column refers to the names of the columns in the MySQL datasheet. You can also use variables.

So with a simple while loop, we can easily output the data to the browser.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.