Skip to main content

Posts

Showing posts from July 2, 2023

How To create Chat Server using - PHP with MYsql db connectivity

//-------------- Chat server BY Kilvish ------------ <?php // Establish MySQL database connection $servername = "localhost" ; $username = "root" ; $password = "" ; $dbname = "chatdb" ; $conn = new mysqli ( $servername , $username , $password , $dbname ); if ( $conn -> connect_error ) {     die ( "Connection failed: " . $conn -> connect_error ); } // Retrieve chat messages from the database function getChatMessages () {     global $conn ;     $sql = " SELECT * FROM chat_messages ORDER BY created_at ASC " ;     $result = $conn -> query ( $sql );     $messages = array ();     if ( $result -> num_rows > 0 ) {             while ( $row = $result -> fetch_assoc ()) {             $messages [] = $row ;         }     }     return $messages ; } // Insert a new chat message into the database function insertChatMessage ( $username , $message ) {     global $conn ;     $sql = "