Home Backend Development PHP Tutorial Detailed explanation of the interactive use of PHP and MySQL_PHP tutorial

Detailed explanation of the interactive use of PHP and MySQL_PHP tutorial

Jul 21, 2016 pm 04:06 PM
mysql php and interaction generation use create database of automatic Detailed explanation connect

Detailed explanation of the interaction between PHP and MySQL
1. Create the code to automatically connect to the database and generate some necessary codes. If we carefully study the connection function of the database, we will find this line of code.
$link_id=@mysql_connect($hostname,$username,$password);
So we just 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 is run. Because our program is interactive and we need to process the information entered by the user, the following code should also be added to the file.
if (count($HTTP_GET_VARS)) /*If the user information is input in GET mode, read the data*/
{ while ( list($key, $value) = each ($HTTP_GET_VARS)) /*Function list() and each() cooperate to process input data*/
{ $arr_request[strtolower($key)] = $value; } }
/*The function strtolower() converts the distinguishing key string to lowercase, which is beneficial to subsequent programming, and forms them into an array*/
if (count($HTTP_POST_VARS)) /*User Information is entered in POST mode*/
{ while (list($key, $value) = each ($HTTP_POST_VARS))
{ $arr_request[strtolower($key)] = $value; } } //We Also 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 also an error message processing
function html_error_exit($msg){ $errno = mysql_errno(); /*Get the error message code*/
$error = mysql_error(); /*Get the error information, the two work together for troubleshooting*/
echo 'Error';echo $msg;
echo "
Error: ($errno) $error
";echo '';exit(); }?>
Okay! Let’s do it Some commonly used codes are placed here, which is convenient to use. 2. There are two methods for creating database tables: entering commands in a DOS environment, but it is easy to make mistakes.
Using programs, even if mistakes are made, it is easy to modify them. .We use a program to create a data table. Because our program needs to be universal, the fields in the table are not important. Here we just simply create one. The table has the following management fields:
key_script This is a An auto-increment field, which ensures that the records in the table are unique. date_created This is a date field, which stores the time when the record was created
data_updated This is also a date field, which stores the time when the record was last updated
flag_deleted stores whether the record has been deleted, "Y": the record has been deleted, "N": the record has not been deleted, you can use the following fields to store information. script_name program name
script_size program bytes script_describe program Brief description author_name program author name author_email program author's email
author_homepage Create the program under the program author's homepage: createTable.php$str_sql="create table php_script(
key_script int(10) unsigned DEFAULT '0' NOT NULL auto_increment,
date_created datetime DEFAULT '0000-00-00 00:00:00',
date_updated datetime DEFAULT '0000-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! Our The table is built! 3. Generate the insert record code program. It seems that we should display the record first and then insert the record, but since we don't have a record yet, we put this step first.
First, create an HTML form to allow users to enter relevant information. Second, create the MySQL code that can insert the form information.good! Let's start. The form style is as follows: Program name: File size: Program description: Author name:
Author email address: Author's homepage: The MySQL code that can insert form information is as follows: script_insert_action.phprequire( 'connect.inc');if($arr_request['action']=='insert'){
$current_date=date('Y-m-d H:i:s');/*Press the current time to YYYY-MM -DD ​​HH:MM:SS arrangement*/
/*The SQL code will be dynamically generated below, in which the auto-increment fields we define are generated by MySQL itself*/
/*In addition, the default value of the flag_deleted field It's "N", so we don't need to mention these two items here*/
/*Everyone knows: PHP strictly distinguishes the functions of single quotes (') and double quotes ("). And our author The name is in the array*/
/*We need to reference the array like this: $arr_request['author_name'], note that there are single quotes (')*/
/*When we enter the value of the insert statement It should be like this: VALUES('$current_date') */
/*If we don't deal with these semicolons, this will happen: VALUES('$arr_request['author_name']') */
/*Can PHP handle this situation? Of course not, so we think of ways to deal with it*//*Here, we use the following technique to avoid this problem; of course, there are other methods you can think of first. Think about it! */
$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'];/*With this replacement, the processing will be 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) ;/* Here is a simple feedback to the user*/
if (!$result){html_error_exit('MySQL insertion command failed!');}else(html_header('Success');
echo"< center> ";echo('MySQL insertion command successful');echo"
";echo"html_footer();)?>
OK! The insert record function is completed!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315361.htmlTechArticleDetailed explanation of the interaction between PHP and MySQL 1. Create the code to automatically connect to the database and generate some necessary code. We carefully If you study the connection function of the database, you will find that this is a line of code...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

See all articles