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.
$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>";
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.
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.