This section describes how to use php and mysql in combination.
Basically, php is a middle layer between the back-end database and the front-end browser, and commands are passed between them. This method greatly improves the possibility of interaction and can be easily used in the voting system and other dynamic user input and personalized websites.
To achieve this interaction, you must first connect to the mysql database. You can use this command to achieve:
Syntax: int mysql_connect (string hostname, string username, string password );
· Hostname-name of the host where the database server is running.
· Username-name of the user connected to the database server.
· Password-user Password. The password set to connect to the MySQL database.
If the connection is successful, the function returns a positive integer. If the connection fails, a negative number is returned.
All commands, as usual, must be placed in "<? "And"?> .
Let's continue with our project. Let's assume that the following data table is created using MySQL:
----------------------------
Mysql> create table information (
-> Name VARCHAR (25 ),
-> Email VARCHAR (25 ),
-> Choice VARCHAR (8 ));
----------------------------
Now let's assume that we want to insert user information into this database. We can modify the email. php3 script to implement it. The modification is as follows:
----------------------------
<?
/* This script uses the variable passed from the moreinfo.html file. */
/* Declare some related variables */
$ Hostname = "devshed ";
$ Username = "myusername ";
$ Password = "mypassword ";
$ DbName = "mydbname ";
/* Access information from a data table created using MySQL */
$ Userstable = "information ";
/* Email address of the website administrator */
$ Adminaddress = "administration@buycorn.com ";
/* Connect to the database */
MYSQL_CONNECT ($ hostname, $ username, $ password) or die ("Unable to connect to database ");
@ Mysql_select_db ("$ dbName") or die ("Unable to select database ");
PRINT "<CENTER> ";
PRINT "Hello, $ name .";
PRINT "<BR> ";
PRINT "Thank you for your interest. <BR> ";
PRINT "We will send information to $ email, and have noted that you like $ preference ";
PRINT "</CENTER> <BR> ";
/* Send related emails */
Mail ("$ email", "Your request for information ",
"$ NamenThank you for your interest! N
We recommend fresh corn daily over the Internet!
Place your order at http://www.buycorn.com,
And receive a free package of $ preference! ");
Mail ("$ adminaddress ",
"Visitor request for info .",
"$ Name requested for information. n
The email address is $ email. n The visitor prefers $ preference .");
/* Insert data into the data table */
$ Query = "insert into $ userstable VALUES ('$ name',' $ email ',' $ preference ')";
$ Result = MYSQL_QUERY ($ query );
PRINT "Your information has also been inserted into our database, for future reference .";
/* Close the connection to the database */
MYSQL_CLOSE ();
?>
----------------------------
Notes:
1. the variables declared at the beginning of the script are for the function MYSQL_CONNECT (). We can also give these values directly in the function. However, considering the development of the project, these values should be placed in an independent file and transferred using (# include.
2. Syntax: int mysql_select_db (string database_name, int link_identifier );
· Database _ name must be the database name on the server.
· Link _ identifier (optional) indicates a connection based on which a request is sent to the database server.
· Return value: true/false
3. Syntax: int mysql_query (string query, int link_identifier );
· Query: SQL commands sent to the mysql server.
· Link _ identifier (optional) indicates the connection, based on which SQL commands are sent to the database server.
· The return value is an integer. A positive value indicates success, and a negative value indicates failure.
4. Syntax: int mysql_close (int link_identifier );
· Link _ indentifier is the same as above
· The return value is an integer. A positive value indicates success, and a negative value indicates failure.
In the next article, I will explain how to output data from mysql.