How to use PHP to create a message system

Source: Internet
Author: User
We can use the message system to store messages. we can add, delete, and hold messages, which reduces the workload of everyone. First, create a table. Createtablenews (n_idint (255) notnullauto_ I

We can use the message system to store messages. we can add, delete, and hold messages, which reduces the workload of everyone.

First, create a table.
Create table news (
N_id int (255) not null auto_increment,
N_date datetime not null,
News text not null,
Primary key (n_id)
);

Step 2: Set your login information
$ Database_user_name = 'root ';
$ Database_password = '';
$ Database_name = 'news ';
$ Time_offset = '0 ';

Step 3: Let's make the things used by the subsequent programs into functions and save time!

Function connect_db ()
{
// Connects to the database

Global $ database_user_name, $ database_password;
$ Db = mysql_connect ('localhost', $ database_user_name, $ database_password );

Return $ db;

}

Function db_name ()
{
// Returns the name of the database

Global $ database_name;
$ Db_name = $ database_name;

Return $ db_name;

}

Function get_now ()
{
// Gets current date and time

$ Db = connect_db ();
$ Db_name = db_name ();
Mysql_select_db ($ db_name, $ db );
$ SQL = 'Select now () as now ';
$ Result = mysql_query ($ SQL, $ db );
$ Myrow = mysql_fetch_array ($ result );
$ Now = $ myrow ['Now'];

Return $ now;

}
Step 4: Let's consider how to display messages

// The function library defined above...
// Table definition ......
  

$ Title ';
If ($ admin = 1)
{

Echo 'admin ';
}

Echo '-News
  
';

$ Db = connect_db ();
$ Db_name = db_name ();
Mysql_select_db ($ db_name, $ db );

If ($ show_news_sub)
{
$ Show_news = $ show_news_sub;
}

If (! Ereg ('([0-9] {'. strlen ($ show_news). '})', $ show_news ))
{
$ Show_news = 'all ';
}

If (strtoupper ($ show_news) = 'all ')
{

$ SQL = 'SELECT date_format (n_date, '% m/% d/% Y') as n_date, date_format (n_date,' % H: % I ') as n_time, news, n_date as date from news order by date desc ';

}
Else
{

$ Link = 'All news ';

  
$ SQL = 'SELECT date_format (n_date, '% m/% d/% Y') as n_date, date_format (n_date,' % H: % I ') as n_time, news, n_date as date from news order by n_date desc limit $ show_news ';
}
$ Result = mysql_query ($ SQL, $ db );
$ Num_rows = mysql_num_rows ($ result );
If ($ num_rows! = 0)
{
Echo'

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.