Interaction between PHP and MySQL

Source: Internet
Author: User
Tags key string mysql code
For details about the interaction between PHP and MySQL, refer to the interaction between PHP and MySQL. for details about the interaction between PHP and MySQL, refer to 1. create the code for automatically connecting to the database and generate some necessary code. let's take a closer look at the database connection functions and we will find such a line of code. $ Link_id = @ mysql_connect ($ hostname, $ username, $ password)
1. create the code for automatically connecting to the database and generate some necessary code. let's take a closer look at the database connection function and we will find such a line of code.
$ Link_id = @ mysql_connect ($ hostname, $ username, $ password );
So we can add the following code to the include file connect. inc. Connect. inc $ Username = 'phpstar'; $ password = 'phpstar'; $ dbname = 'script ';
$ Tablename = 'php _ script'; $ link_id = mysql_connect ($ hostname, $ username, $ password );
If (! $ Link_id) {echo' Error';
Echo 'connection to PHP has failed. '; echo''; Exit () ;}?>
Add this program to every PHP script, so that a database connection is established when the script runs. Because our program is interactive, we need to process user input information, so the following code should be added to this file.
If (count ($ HTTP_GET_VARS)/* if the user information is input in GET mode, the data is read */
{While (list ($ key, $ value) = each ($ HTTP_GET_VARS)/* function list () and each () are used together to process input data */
{$ Arr_request [strtolower ($ key)] = $ value ;}}
/* The strtolower () function converts the distinguished key string to lowercase, which is good for subsequent programming and makes them an array */
If (count ($ HTTP_POST_VARS)/* enter user information in POST mode */
{While (list ($ key, $ value) = each ($ HTTP_POST_VARS ))
{$ Arr_request [strtolower ($ key)] = $ value ;}// we also need to define the HTML output each time.
Function html_header ($ title) {echo' '; Echo "$ title"; <br> echo'';} Function html_footer ()
{Global $ link_id; @ mysql_close ($ link_id); echo'';} // There is an error message for processing
Function html_error_exit ($ msg) {$ errno = mysql_errno ();/* get error code */
$ Error = mysql_error ();/* get the error message, which can be used together for troubleshooting */
Echo' Error'; Echo $ msg;
Echo"
Error: ($ errno) $ error
"; Echo''; Exit () ;}?>
Okay! We put some common code here, so it is easy to use 2. there are two ways to create a database table: enter commands in the DOS environment, but it is prone to errors.
It is easy to modify the program even if an error occurs. we use a program to create a data table. because our program needs to be universal, the field in the table is not important. here we just create a simple. the table has the following management fields:
Key_script is a field with automatic increments, which ensures that the records in the table are unique. date_created is a date field that stores the time when the record was created.
Data_updated, which is also a date field, stores the last update time of the record.
Flag_deleted: whether the record is deleted. "Y": the record has been deleted. "N": the record has not been deleted. you can use the field. script_name with the following storage information.
Script_size the number of bytes in the program script_describe a simple description of the program author_name program author name author_email program author email
Create this program under the home page of the author of the author_homepage program: createTable. php $ Str_ SQL = "create table php_script (
Key_script int (10) unsigned DEFAULT '0' not null auto_increment,
Date_created datetime DEFAULT '2017-00-00 00:00:00 ',
Date_updated datetime DEFAULT '2017-00-00 00:00:00 ',
Flag_deleted enum ('Y', 'n') DEFAULT 'n' not null,
Script_name VARCHAR (20) not null, script_size VARCHAR (10) not null,
Script_describe VARCHAR (200) not null, author_name VARCHAR (20) not null,
Author_email VARCHAR (20) not null, author_homepage VARCHAR (30) not null,
Primary key (key_script) "; $ result = mysql_db_query ($ dbname, $ str_ SQL, $ link_id );
If ($ result) {echo "OK! Table $ tablename has been created! ";} Else {echo" Failed! ";}
?> OK! We have created the table! 3. Generate the insert record Code program. It seems that we should first display the record and then insert it into the record, but because we have no record, we have mentioned this step above.
First, create an HTML form for users to enter relevant information. Create a MySQL code that can insert form information. Good! The form style is as follows: program name: file size: Program Description: Author name:
The author's email address: The author's homepage: the MySQL code that can insert form information is as follows: script_insert_action.php Require ('connect. Inc'); if ($ arr_request ['action'] = 'insert '){
$ Current_date = date ('Y-m-d H: I: s');/* sort the current time in YYYY-MM-DD HH: MM: SS mode */
/* The following code is generated dynamically. the automatically incremental fields defined by us are generated by MySQL */
/* In addition, the saved value of the flag_deleted field is "N", so we do not need to mention these two items here */
/* Everyone knows that PHP strictly distinguishes between single quotation marks (') and double quotation marks. And our author name is in the array */
/* We need to reference the array like this: $ arr_request ['author _ name']. Note that there are single quotation marks (')*/
/* The value of the insert statement should be as follows: VALUES ('$ current_date ')*/
/* If we do not process these semicolons, this will happen: VALUES ('$ arr_request ['author _ name']') */
/* Can PHP handle this situation? Of course not, so we want to solve it. * // * here, we use the following technology to avoid this problem. of course, there are other methods here. Think about it first! */
$ Script_name = $ arr_request ['script _ name'];
$ Script_size = $ arr_request ['script _ size'];
$ Script_describe = $ arr_request ['script _ describe '];
$ Author_name = $ arr_request ['author _ name'];
$ Author_email = $ arr_request ['author _ email '];
$ Author_homepage = $ arr_request ['author _ homepage'];/* This is a replacement, and the processing is much better */
$ Str_ SQL = "insert into $ tablename (date_created, date_updated, script_name,
Script_size, script_describe, author_name, author_email, author_homepage) VALUES (
'$ Current_date', '$ current_date', '$ script_name', '$ script_size ',
'$ Script_describe', '$ author_name', '$ author_email', '$ author_homepage ')";
$ Result = mysql_db_query ($ dbname, $ str_ SQL, $ link_id);/* simple information feedback for users */
If (! $ Result) {html_error_exit ('MySQL insertion command failed! ');} Else (html_header ('success ');
Echo" "; Echo ('MySQL command inserted successfully'); echo"
"; Echo" html_footer ();)?>
OK! The record insertion function is complete!

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.