PHP + MYSQL message book

Source: Internet
Author: User
& Nbsp; I think the most important thing to learn about programming is hands-on practice. I learned php on the first day of today ~~ '~ To familiarize yourself with this language ~~~ I began to compile a message book for PHP + MySQL myself. & nbsp; from easy to difficult '~~~ Start with a simple one. the function will be improved later. & nbsp; the most basic function of the message book is: & nbsp; 1: the user writes the message & nbsp; 2: write the data to the database & nbsp; 3: show all messages & learn programming, I think the most important thing is hands-on practice. learn php on the first day of today ~~ '~ To familiarize yourself with this language ~~~ I began to compile a message book for PHP + MySQL myself.
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 under PHPMYADMIN, and then create a contents table under this database to create two fields under this table
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,
Primary 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 (message display page) updata. php (the page that writes 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_connect ("localhost: 6033", "root ","");
Mysql_query ("set names UTF-8"); // solves Chinese garbled characters
Mysql_select_db ("guest_book ");
$ Exec = "insert into contents (name, content) values ('". $ _ POST ['User _ name']. "','". $ _ POST ['post _ contents']. "')";
$ Result = mysql_query ($ exec );
?>


The index. php page code is as follows:

$ Conn = mysql_connect ("localhost: 6033", "root", ""); // open the MySQL server connection
Mysql_select_db ("guest_book"); // link to the database
Mysql_query ("set names UTF-8"); // solves Chinese garbled characters
$ Exec = "select * from contents"; // SQL statement
$ Result = mysql_query ($ exec); // execute the SQL statement and return the result
While ($ rs = mysql_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_connect ("localhost: 6033", "root ","");
At the beginning, I used $ conn = mysql_connect ("127.0.0.1 ","","");
How can I avoid getting data into the database ~~~~ But no error is reported ~~ It took only half a day to know which 127 of the original port should be checked in PHPMYADMIN. what is the database port with a single blow to the server name ~~~ Also, the ROOT user name is the mysql User name, followed by the password.

Another problem is Chinese character garbled characters.

Add mysql_query ("set names gb2312") before $ result = mysql_query ($ exec); or mysql_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.

Sometimes the Chinese characters cannot be displayed normally after this setting ~~~~
I met this situation. because I debug it locally, I need to adjust the character encoding in the browser to UTF-8 each time to display Chinese characters normally.
The default encoding is always ISO-8859-1, so google '~ It turns out that apache is not set correctly. therefore, find the httpd. conf file.
Change default-character-set = ISO-8859-1 to default-character-set = UTF-8
Then restart the service httpd restart and appache to clear all cookies and history records ~~~ Then the problem is solved.

Now, I will write it here today '~ 'Continue to improve this message book tomorrow ~~~
Http://blog.csdn.net/sputnik/archive/2007/02/11/1507954.aspx

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.