PHP combined with MySQL database to implement message board function _php Example

Source: Internet
Author: User
Tags php connect to mysql database
First show you the message board:


Recently read PHP basic grammar, want to use these basic things to implement the message board, but also a solid foundation of knowledge.

What is a message board? A carrier that can be used to record and display text messages.

Now cut to the point, say this message board is how to achieve!

First, the user submits the message, the relevant content into the server, when he wants to see the backstage and then all the messages read out, finally displayed in the browser, the user can see the message.

This one of the backstage needs a tool that is easy to read and write data, I choose MySQL database to help me accomplish these things.

I wrote the main is three PHP files, respectively:

conn.php Connection database;

addmsg.php PHP read the message from the page, and put it into the database (Insert);

listmsg.php reads the message from the database and displays it on the page;

1. To prepare the structure of the database table , here is my table structure under phpMyAdmin:


Build table Syntax

SQL CREATE TABLE syntax create TABLE table name (column name 1 data type, column name 2 data type, column name 3 data type,....)

2.php Connect to MySQL database, and then select one of the databases, I chose here is the BBS database (PS created before) The following describes a few PHP library functions to use,

Copy the Code code as follows:
①mysql_connect ("localhost", "root", "" ")

PHP connection MySQL, the parameters are MySQL address (localhost for natively), user name, password

Return value: If the connection fails to return FALSE, a connection identifier is returned successfully

Copy the Code code as follows:
②mysql_select_db ($dbName, $conn);

MySQL can have a lot of db, so you need to select one of the DB for the next operation.

Parameters: The first is the database name, the second is the link identifier, you can put the return value in ① here, which means I will use MySQL in ①.

Return value: False connection failed, true connection succeeded.

Copy the Code code as follows:
③mysql_query (query,connection)

Parameter: Query represents the statement you want MySQL to execute

Connection optional, SQL connection identifier as above

return value: mysql_query () returns a resource identifier only for a select,show,explain or describe statement, or FALSE if the query is executed incorrectly.

For other types of SQL statements, mysql_query () returns TRUE on successful execution and returns FALSE on error.

Personal summary of this return value: This function fails to return false, execution succeeds to see what statement, if it is a select,show,explain or DESCRIBE statement, then will return the resource identifier, the other statements will return true;

Having said so much, the context of the message board has come out.

Start with 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 code; function Tohtmlcode ($content) {return $content = Str_replace ("\ n", "
", Str_replace (" "," ", $content));}? >

There's a Tohtmlcode custom function that replaces a carriage return (\ n) in a string with a line break in HTML
, replace the space with spaces in HTML ()
One of the functions is described below

Grammar

Copy the Code code as follows:
Str_replace (Find,replace,string,count)

Parameters Description
find
replace
string
Count Optional. A variable that counts the number of replacements.

addmsg.php

<?php//Referencing the connection database file ("conn.php") that was written before; if (@$_post[' submit ']) {$sql = "INSERT into message (id,user,title,content,lastdate)". "Values (' ', ' $_post[username] ', ' $_post[title] ', ' $_post[content] ', now ())"; mysql_query ($sql); echo "Add success"; }?>   

Include is the introduction of conn.php, similar to the C language include

The $_post variable is an array that collects the values from the form method= "POST", the value of the key value that the post emits in this $_post array $_post[' submit ' key, and if the submit is triggered, That is, when Checkpost returns to True, the POST value is $_post[, obviously, ' submit ' is not empty and non-null is true, then the INSERT statement inside the IF is executed. Keep the message content in the MySQL database.

listmsg.php

<?php include ("conn.php");?> 
  
   
  
   
    
    <?php $sql = "SELECT * from Message ORDER by lastdate DESC"; $query = mysql_query ($sql); while ($row = Mysql_fetch_array ($query)) {?> 
   
    
   <?php}?> 
   
Title: <?= $row [' title ']?> User: <?= $row [' User ']?>
Content: <?= tohtmlcode ($row [' content '])?>

PHP and HTML code mixed seems to be quite messy.

PHP gets the message from MySQL and displays it on the page, which I'll show in the table. The main code is above.

The above is a small series to share the PHP with MySQL database to achieve the function of the message board, we hope to help!

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