Interaction between PHP4 and MySQL

Source: Internet
Author: User

1. Establish a connection to the MySQL database.

First, we should create a database in MySQL to create an infostar table:

Follow the steps to test MySQL, see the "mysql>" prompt and manually create the table we need:

Mysql> create database mydb

Mysql> USE mydb

Mysql> create table infostar (
-> Name VARCHAR (25 ),
-> Email VARCHAR (25 ),
-> Choice VARCHAR (10 ));

Now, the table is created. Next we will first create a form for meeting users (a pure HTML file)

Diaocha.htm

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>

<Body bgcolor = "# CCCCCC">
<Center>
<H1> what kind of fruit do you like <Form method = "post" action = "diaocha. php">
<Table border>
<Tr> <td> Name:
<Td>
<Input type = "text" name = "name" size = "20" maxlength = "20" value = "">
<Tr> <td> Email:
<Td>
<Input type = "text" name = "email" size = "20" maxlength = "20" value = "">
<Tr> <td> select:
<Td>
<Input type = "radio" name = "choice" value = "apple"> apple
<Input type = "radio" name = "choice" value = "orange"> orange
<Input type = "radio" name = "choice" value = "pear"> pears
<Br>
<Input type = "radio" name = "choice" value = "coconut"> coconut
<Input type = "radio" name = "choice" value = "watermelon"> watermelon
<Input type = "radio" name = "choice" value = "banana"> bananas
</Table>
<P>
<Input type = "submit" name = "submit_button" value = "OK">
<Input type = "reset" name = "reset_button" value = "reset">
</Form>
</BODY>
</HTML>

The format is as follows:

What kind of fruit do you like
Name:
Email:
Please select: Apple orangli
Coconut, watermelon, and bananas




Next, create a PHP program to receive and process the information sent from the diaocha.htm form.

Diaocha. php

<? Php
/* Define related variables for the mysql_connect () function below */
$ Hostname = "localhost ";
$ Username = "root ";
$ Password = "";
$ Dbname = "mydb ";

/* Used to store the Site supervisor's email address */
$ Adminaddress = "webmaster@youweb.com ";

/* This is the table we just created to store user data */
$ Userstable = "infostar ";

/* Establish a connection below */
Mysql_connect ($ hostname, $ username, $ password) or die ("Unable to connect database ");

/* Select database mydb. Adding the (@) symbol before the function will suppress the display of error information .*/
@ Mysql_select_db ("$ dbname") or die ("Unable ro select database ");

Print "<center> ";
Print "Hello, $ name .";
Print "<br> ";
Print "your Email is: $ email <br> ";
Print "Thank you for your participation <br> ";


/* Insert information to the infostar table */
/* Because the database table is selected using the mysql_select_db function, the following table name does not need to be input */
$ Query = "insert into $ userstable VALUES ('$ name',' $ email ',' $ choice ')";
$ Result = mysql_query ($ query );

Print "your information has been saved to the Database .";

/* Close the database connection */
Mysql_close ();
?>

Note: For programming, if you define string variables at the beginning, it will be easy to modify.

We have stored the information in the database. How can we view the data?
Next, we will try to list all our friends who like to eat apple and create an apple. php file.

Apple. php

<? Php
/* Define related variables for the mysql_connect () function below */
$ Hostname = "localhost ";
$ Username = "root ";
$ Password = "";
$ Dbname = "mydb ";
$ Userstable = "infostar ";

/* Create a connection and select a database */
Mysql_connect ($ hostname, $ username, $ password) or die ("Unable to connect database ");
@ Mysql_select_db ("$ dbname") or die ("Unable ro select database ");

/* Select all users who like Apple */
$ Query = "SELECT * FROM $ userstable WHERE choice = 'apple '";
$ Result = mysql_query ($ query );

/* Calculate the number of such users */
$ Number = mysql_num_rows ($ result );

/* Display the result */
$ I = 0;
IF ($ number = 0 ){
Print "<center> <p> no one liked apple </center> ";}
ELSE {
Print "<center> <p> Users who like Apple: $ number <br> ";
WHILE ($ I <$ number ):
$ Name = mysql_result ($ result, $ I, "name ");
$ Email = mysql_result ($ result, $ I, "email ");
Print "$ name like Apple <br> ";
Print "email address: $ email <br> ";
$ I ++;
Endwhile;
Print "</center> ";
}
?>

Call apple. php In the browser to see what is going on.

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.