Php+mysql+ajax Lightweight Chat Room implementation method _php skills

Source: Internet
Author: User

This article describes the implementation method of Php+mysql+ajax lightweight chat room. Share to everyone for your reference, specific as follows:

Do a QQ chat dating site, want to add a chat function, so finished using PHP to do a simple and powerful chat room

1. Create MySQL database table:

Copy Code code as follows:
CREATE TABLE Chat (id bigint auto_increment,username varchar (), chatdate datetime,msg (varchar), primary key (ID));

2. Write the recommended connection database function:

dbconnect.php

<?php
function Db_connect ()
{
 date_default_timezone_set ("Asia/shanghai");
 $link = mysql_connect ("xxx.xxx.xxx.xxx", "databasename", "password")
      or Die (' Cannot connect: '. Mysql_error ());
 mysql_select_db ("DatabaseName") or Die (' not you find the specified database ');
 return true;
}
function quote ($strText)
{
  $Mstr = addslashes ($strText);
  Return "'". $Mstr. "'";
}
function IsDate ($d)
{
  $ret = true;
  Try
  {
    $x = date ("D", $d);
  }
  catch (Exception $e)
  {
    $ret = false;
  }
  echo $x;
  return $ret;
>

3. Write Ajax send and receive functions:

Ajax Send function chat_send_ajax.php

<?php
   require_once (' dbconnect.php ');
   Db_connect ();
   $msg = Iconv ("UTF-8", "GB2312", $_get["MSG"]);
   $DT = Date ("y-m-d h:i:s");
   $user = Iconv ("UTF-8", "GB2312", $_get["name"]);
   $sql = "INSERT into chat (username,chatdate,msg)".
     " VALUES (". QUOTE ($user). "," . QUOTE ($DT). "," . QUOTE ($MSG). ");";
     echo $sql;
   $result = mysql_query ($sql);
   if (! $result)
   {
    throw new Exception (' Query failed: '. mysql_error ());
    Exit ();
>

Ajax receive Function chat_recv_ajax.php

 <?php header ("content-type:text/html;charset=gb2312"); Header ("Expires:thu.
  1970 00:00:01 GMT ");
  Header ("Cache-control:no-cache, must-revalidate");
   Header ("Pragma:no-cache");
   Require_once (' dbconnect.php ');
   Db_connect ();
   $sql = "SELECT *, Date_format (chatdate, '%y%m month%d%r ') as CDT from chat ORDER by ID desc limit 200"; $sql = "SELECT * from". $sql.
   ") as Ch order by ID";
   $result = mysql_query ($sql) or Die (' Query failed: '. mysql_error ()); Update Row Information $msg = "<table border= ' 0 ' style= ' font-size:10pt; Color:white; Font-family:verdana, Arial;
   > "; while ($line = Mysql_fetch_array ($result, Mysql_assoc)) {$msg = $msg. "<tr><td>". $line [CDT].
        "</td>". "<td>". $line ["username"].
        ": </td>". "<td>". $line ["MSG"].
   "</td></tr>"; } $msg = $msg.
   "</table>"; echo $msg;?> 

4. Chat Room page:

Chat.html

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

The effect chart is as follows:

More about PHP interested readers can view the site topics: "Php+mysql Message Board Development topic", "Php+ajax Tips and Applications Summary", "PHP file Operation Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Related Article

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.