PHP + MYSQL message book

Source: Internet
Author: User
Tags myadmin
& Nbsp; I think the most important thing to learn about programming is hands-on practice. I learned php on the first day of today (as the mainstream development language )~~ '~ To familiarize yourself with this language ~~~ I began to compile a message book for php (as the mainstream development language) + MySQL (the best combination with PHP) by myself. & nbsp; from ease to difficulty '~~~ Start with simple functions. SyntaxH

I think the most important thing to learn about programming is hands-on practice. I learned php on the first day of today (as the mainstream development language )~~ '~ To familiarize yourself with this language ~~~ I began to compile a message book for php (as the mainstream development language) + MySQL (the best combination with PHP.
From easy to difficult '~~~ Start with a simple one. The functions will be improved later.

The most basic functions of a message book are:
1: the user writes a message
2: write data to the database
3: show all messages

Next, we will start to prepare my message book.

First, create a guest_book database in php (as the mainstream development language) MYADMIN, and then create a contents table under the database to create two fields.
Name and content
The SQL statement is as follows:
Create table 'Contents '(
'Id' int (11) not null auto_increment,
'Name' varchar (20) not null default "no name ",
'Content' mediumtext not null,
Prima (the most comprehensive VM Management System) ry key ('id ')
) TYPE = MyISAM AUTO_INCREMENT = 6

The database has been created ~~ 'Write the program below
This program contains three pages: post.htm (message submission page) index. php (as the mainstream development language) (message display page) updata. php (as the mainstream development language) (the page for writing data to the database)

The post.htm code is as follows:



Message book

 

 

The updata. php page code is as follows:
$ Name = $ _ POST [user_name];
$ Content = $ _ POST [post_contents];
$ Conn = MySQL (the best combination with PHP) _ connect ("localhost: 6033", "root ","");
MySQL (the best combination with PHP) _ query ("set names UTF-8"); // solves Chinese garbled characters
MySQL (best combination with PHP) _ select_db ("guest_book ");
$ Exec = "insert into contents (name, content) values (". $ _ POST [user_name]. ",". $ _ POST [post_contents]. ")";
$ Result = MySQL (the best combination with PHP) _ query ($ exec );
?>


The index. php page code is as follows:

$ Conn = MySQL (the best combination with PHP) _ connect ("localhost: 6033", "root ",""); // open the MySQL (best combination with PHP) server connection
MySQL (best combination with PHP) _ select_db ("guest_book"); // link to the database
MySQL (the best combination with PHP) _ query ("set names UTF-8"); // solves Chinese garbled characters
$ Exec = "select * from contents"; // SQL statement
$ Result = MySQL (the best combination with PHP) _ query ($ exec); // execute the SQL statement and return the result
While ($ rs = MySQL (the best combination with PHP) _ fetch_object ($ result ))
{
Echo"


";Echo"
Name: ". $ rs-> name ."
Message: ". $ rs-> content ."

";
}?>


As for paging, page turns and other functions are not used for the moment. The only difference is that the program should be streamlined as much as possible. although it is small, the core functions of the message book are all here.

A few more words are required.

$ Conn = MySQL (the best combination with PHP) _ connect ("localhost: 6033", "root ","");
At the beginning, I used $ conn = MySQL (the best combination with PHP) _ connect ("127.0.0.1 ","","");
How can I avoid getting data into the database ~~~~ But no error is reported ~~ Later, I came to know which 127 of the original database should be used in php (as the mainstream development language). in MYADMIN, I checked what the server name hit database port is ~~~ Also, the ROOT user is the MySQL (the best combination with PHP) user name, followed by the password

Another problem is Chinese character garbled characters.

Add MySQL before $ result = MySQL (the best combination with PHP) _ query ($ exec); statement (the best combination with PHP) _ query ("set names gb2312"); or MySQL (optimal combination with PHP) _ query ("set names UTF-8 ");
Suspicious: prevents Chinese characters submitted to the database from being stored in the database in garbled format, and prevents data containing Chinese characters from being queried from the database from being displayed in garbled format.

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.