Use PHP + Mysql to implement the message board function

Source: Internet
Author: User
PHP + Mysql implementation of the message board recently read the basic PHP syntax, just want to use these basic things to achieve the message board, 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 the structure of the database table. below is the structure of my table under 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 php to the mysql database and select one of the databases. here I select the bbs database (created before ps). The following describes several php database functions to be used,


① 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.

② 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.

③ 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

 ", 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

str_replace(find,replace,string,count)

Addmsg. php

     

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.