PHP3 Introductory Tutorials---Where to look.

Source: Internet
Author: User
Tags learn php mail mysql variables php and query mysql database

1. The variables defined at the beginning of the script are the parameters of the mysql_connect () function, and of course we can insert those strings directly into the function, but in a large WEB application, these values are likely to be placed in several different files and then included (with the Include statement) , if you start by defining these string variables, it's easy to modify them.

2. Function @mysql_select_db () is used to select a database. This saves some time and enables you to execute query statements without giving the database name.

Syntax: int mysql_select_db (string database_name, int link_identifier);

* database_name must be a database name on the server.

* Link_identifier (optional) indicates the established database connection number, and if omitted, the last open connection is used.

* Returns TRUE/False values based on success or failure of execution.

3. Function mysql_query () is used to send a query to the MYSQL database:

Syntax: int mysql_query (string query, int link_identifier);

* Query-the SQL string used for querying.

* Link_identifier-database name (optional, if omitted, use the last Open database connection), you must give the database name if you do not want to use the database selected by the function @mysql_select_db ().

* Returns positive/negative values based on success or failure, if the SELECT query is executed, then returns the result number, otherwise the return value can be ignored.

4. The Mysql_close function closes the connection to the MYSQL database.

Syntax: int mysql_close (int link_identifier);

* Link_identifier-ditto.

* Similarly, returns positive/negative values depending on the success or failure of the execution.

If set correctly, you will see that the data is actually added to the information table. In the next installment, we'll learn how to extract data from a MySQL database and then show it.

Extracting data from MySQL

We have successfully obtained enough user information and are stored in the database. But how do you navigate the data and get useful conclusions from it?

Next, we want to list the names and mailing addresses of all the users who like Apple:



/* This script is used to show the name and email address of all users who like Apple.

/* Define some relevant variables * *
$hostname = "devshed";
$username = "MyUserName";
$password = "MyPassword";
$userstable = "Information";
$dbName = "Mydbname";

/* Establish a connection * *
Mysql_connect ($hostname, $username, $password) or DIE ("Unable to CONNECT to database");

@mysql_select_db ("$dbName") or Die ("Unable to select database");

* * All users who like Apple.
$query = "SELECT * from $userstable WHERE choice = ' Apples '";

$result = mysql_query ($query);

/* Calculate how many of these users * *
$number = Mysql_numrows ($result);

/* The results are displayed on the screen * *
$i = 0;

IF ($number = = 0):
PRINT "< center>< p> no one likes to eat Apple </center>";
ELSEIF ($number > 0):
PRINT "< center>< p> like to eat Apple users: $number < br>< br>";
while ($i < $number):
$name = mysql_result ($result, $i, "name");
$email = mysql_result ($result, $i, "email");
PRINT "$name like Apple < br>";
PRINT "mail address: $email.";
PRINT "< br>< br>";
$i + +;
Endwhile;
PRINT "</center>";
ENDIF;
?>

Save the result as apples.php3.

Here's an explanation of the functions used:

$number = Mysql_numrows ($result);

Syntax: int mysql_num_rows (string result);

* Results-Returns the result number by the mysql_query function.

* Function return value is the number of records in the record group.

There is also a function similar to this: Mysql_num_fields (string result), which returns the number of record set fields.

In the output process, if the records in the database show that no one likes Apple, then the string "No one likes to eat Apple", no, the output of the user did not search the name and e-mail address. This uses a while loop to output all eligible data.

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

Syntax: int mysql_result (int result, int i, column);

Mysql_result () is the value used to extract a field from a record:

* $result indicates the set of records to be manipulated.

* $i Specify the number of records to be operated in the record set

* column is a field name in the MySQL table structure.

So, with a simple while loop, we can output all the data.

SQL functions:

Using the mysql_query () function, you can perform some SQL functions to manipulate the database, including the DELETE and UPDATE functions:

Delete

Suppose we want to delete a record named "Bunny", you can do this:

$query = "DELETE from $userstable WHERE name =" Bunny ";

mysql_query ($query);

Update

Or we would like to revise the records of all names "Bunny" and Change "Bunny" to ""

$query = "UPDATE $userstable SET name =" Bugs Bunny "WHERE name =" Bunny "; mysql_query ($query);

After reading this article, we should have a general understanding of the PHP3.0. We've seen how to create dynamic Web pages with PHP3.0, and how to publish a database to the web via a combination of PHP3.0 and MySQL. But these are just the tip of the iceberg, and PHP3.0 has many powerful features. Since this is just an introductory introductory article, here is not much to say.

I think the best way to learn PHP is to read PHP3.0 's documents, which are written by the great developers of PHP and may not have much more detailed information than this document. You don't have to memorize it, just read it and understand it. This document and some of the latest information about PHP can be found in the http://www.php.net, this is the lair of PHP, but also learn PHP must go to the side. MySQL documentation and related resources can be found in http://www.mysql.com.

-->



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.