Publishing AJAX and PHP - part 16

AJAX Chat // destructor closes database connection public function __destruct() { $this-mMysqli-close(); } // truncates the table containing the messages public function deleteMessages() { // build the SQL query that adds a new message to the server $query = 'TRUNCATE TABLE chat'; // execute the SQL query $result = $this-mMysqli-query($query); } /* The postMessages method inserts a message into the database - $name represents the name of the user that posted the message - $messsage is the posted message - $color contains the color chosen by the user */ public function postMessage($name, $message, $color) { // escape the variable data. | AJAX Chat destructor closes database connection public functiondestruct this- mMysqli- close truncates the table containing the messages public function deleteMessages build the SQL query that adds a new message to the server query TRUNCATE TABLE chat execute the SQL query result this- mMysqli- query query The postMessages method inserts a message into the database - name represents the name of the user that posted the message - messsage is the posted message - color contains the color chosen by the user public function postMessage name message color escape the variable data for safely adding them to the database name this- mMysqli- real_escape_string name message this- mMysqli- real_escape_string message color this- mMysqli- real_escape_string color build the SQL query that adds a new message to the server query INSERT INTO chat posted_on user_name message color . VALUES NOW . name . . message . . color . execute the SQL query result this- mMysqli- query query The retrieveNewMessages method retrieves the new messages that have been posted to the server. - the id parameter is sent by the client and it represents the id of the last message received by the client. Messages more recent by id will be fetched from the database and returned to the client in XML format. public function retrieveNewMessages id 0 escape the variable data id this- mMysqli- real_escape_string id compose the SQL query that retrieves new messages if id 0 retrieve messages newer than id query SELECT chat_id user_name message color . DATE_FORMAT posted_on Y- m- d H i s . AS posted_on . FROM chat WHERE chat_id . id . ORDER BY chat_id ASC else on the first load only retrieve the last 50 messages from server query SELECT chat_id user_name message color posted_on FROM . SELECT chat_id user_name message color . 150 Chapter 5 DATE_FORMAT posted_on Y- m- d H i s as posted_on . FROM chat . ORDER BY chat_id DESC . LIMIT 50 AS Last50 . ORDER BY chat_id ASC execute the query result this- mMysqli- query .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.