PHP and Mysql database are used to implement the message board function and mysql message board. PHP implements the message board function in combination with the Mysql database. the mysql message board shows you the message board: I recently read the basic PHP syntax and want to use these basic things to implement the message board function in combination with the Mysql database, mysql message board
The following message board is displayed:
I recently read the basic PHP syntax and want to use these basic things to implement the message board, which is also a consolidation of the basic knowledge.
What is a message board? A carrier that can be used to record and display text information.
Now let's get to the point and talk about how to implement this message board!
After the user submits the message, the related content is saved to the server. when he wants to view the message, the background reads all the messages and displays them in the browser. then, the user can see the message.
In this case, the backend needs a tool that is easy to read and write data. I chose the mysql database to help me complete these tasks.
I wrote three php files:
Conn. php connects to the database;
Addmsg. php reads message-related content from the page and saves it to the (Insert) database;
Listmsg. php reads the message content from the database and displays it on the page;
1. prepare to create a database table structureThe following table structure is in phpMyAdmin:
Table creation syntax
SQL CREATE TABLE syntax CREATE TABLE name (column name 1 data type, column name 2 data type, column name 3 data type ,....)
2. connect to the mysql database using php, and select one of the databases.Here I chose the bbs database (created before ps). below I will introduce several php library functions to be used,
The code is as follows:
① Mysql_connect ("localhost", "root ","")
Php connects to mysql. the parameters are the mysql address (localhost represents the local machine), user name, and password.
Returned value: if the connection fails, false is returned. if the connection fails, a connection identifier is returned.
The code is as follows:
② Mysql_select_db ($ dbName, $ conn );
Mysql can have many databases, so you need to select one of them for the following operations.
Parameter: The first is the database name, and the second is the link identifier. you can put the return value in ① here, which indicates that I will use mysql in ①.
Returned value: "false" indicates that the connection fails. "true" indicates that the connection is successful.
The code is as follows:
③ Mysql_query (query, connection)
Parameter: query indicates the statement you want mysql to execute.
Connection (optional) The SQL connection identifier is described above.
Returned value: mysql_query () returns a resource identifier only for SELECT, SHOW, EXPLAIN, or DESCRIBE statements. if the query execution is incorrect, FALSE is returned.
For other types of SQL statements, if mysql_query () is executed successfully, TRUE is returned. If an error occurs, FALSE is returned.
Summary of the returned value: if the function fails to be executed, false is returned. if the function is executed successfully, the resource identifier is returned. if it is a SELECT, SHOW, EXPLAIN, or DESCRIBE statement, the resource identifier is returned, other statements return true;
After talking about this, the context of the message board has come out.
Start the code below
Conn. php
<? Php include ("head. php "); $ dbName =" bbs "; $ conn = @ mysql_connect (" localhost "," root "," ") or die (" database link error "); $ flag = mysql_select_db ($ dbName, $ conn); mysql_query ("set names 'gbk'"); // use GBK Chinese encoding; function toHtmlcode ($ content) {return $ content = str_replace ("\ n ","
", Str_replace (" "," ", $ content) ;}?>
The preceding toHtmlcode UDF replaces the carriage return (\ n) in the string with the line break in html.
To replace spaces with spaces () in html ()
One function is described as follows:
Syntax
The code is as follows:
Str_replace (find, replace, string, count)
Parameters |
Description |
Find |
Required. Specifies the value to be searched. |
Replace |
Required. Required replacementFindValue. |
String |
Required. Specifies the string to be searched. |
Count |
Optional. A variable that counts the number of replicas. |
Addmsg. php
<? Php // reference the previously written connection database file include ("conn. php "); if (@ $ _ POST ['submit ']) {$ SQL =" insert into message (id, user, title, content, lastdate )". "values ('', '$ _ POST [userName]', '$ _ POST [title]', '$ _ POST [content]', now ())"; mysql_query ($ SQL); echo "added successfully" ;}?>