Hi all. I am having one chat script which I used for my site. It was working fine with mysql database. Then after updated PHP 7 with mysqli then suddenly stopped for working. I need to make the code work with PHP 7 + mysqli. Please help me it was very important. Thank you

<?php

define ('DBPATH','localhost');
define ('DBUSER','root');
define ('DBPASS','password');
define ('DBNAME','chat');

session_start();

global $dbh;
$dbh = mysql_connect(DBPATH,DBUSER,DBPASS);
mysql_selectdb(DBNAME,$dbh);

if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); } 
if ($_GET['action'] == "sendchat") { sendChat(); } 
if ($_GET['action'] == "closechat") { closeChat(); } 
if ($_GET['action'] == "startchatsession") { startChatSession(); } 

if (!isset($_SESSION['chatHistory'])) {
    $_SESSION['chatHistory'] = array(); 
}

if (!isset($_SESSION['openChatBoxes'])) {
    $_SESSION['openChatBoxes'] = array();   
}

function chatHeartbeat() {

    $sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['username'])."' AND recd = 0) order by id ASC";
    $query = mysql_query($sql);
    $items = '';

    $chatBoxes = array();

    while ($chat = mysql_fetch_array($query)) {

        if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
            $items = $_SESSION['chatHistory'][$chat['from']];
        }

        $chat['message'] = sanitize($chat['message']);

        $items .= <<<EOD
                       {
            "s": "0",
            "f": "{$chat['from']}",
            "m": "{$chat['message']}"
       },
EOD;

    if (!isset($_SESSION['chatHistory'][$chat['from']])) {
        $_SESSION['chatHistory'][$chat['from']] = '';
    }

    $_SESSION['chatHistory'][$chat['from']] .= <<<EOD
                           {
            "s": "0",
            "f": "{$chat['from']}",
            "m": "{$chat['message']}"
       },
EOD;

        unset($_SESSION['tsChatBoxes'][$chat['from']]);
        $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
    }

    if (!empty($_SESSION['openChatBoxes'])) {
    foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
        if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
            $now = time()-strtotime($time);
            $time = date('g:iA M dS', strtotime($time));

            $message = "Sent at $time";
            if ($now > 180) {
                $items .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;

    if (!isset($_SESSION['chatHistory'][$chatbox])) {
        $_SESSION['chatHistory'][$chatbox] = '';
    }

    $_SESSION['chatHistory'][$chatbox] .= <<<EOD
        {
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
            $_SESSION['tsChatBoxes'][$chatbox] = 1;
        }
        }
    }
}

    $sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['username'])."' and recd = 0";
    $query = mysql_query($sql);

    if ($items != '') {
        $items = substr($items, 0, -1);
    }
header('Content-type: application/json');
?>
{
        "items": [
            <?php echo $items;?>
        ]
}

<?php
            exit(0);
}

function chatBoxSession($chatbox) {

    $items = '';

    if (isset($_SESSION['chatHistory'][$chatbox])) {
        $items = $_SESSION['chatHistory'][$chatbox];
    }

    return $items;
}

function startChatSession() {
    $items = '';
    if (!empty($_SESSION['openChatBoxes'])) {
        foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
            $items .= chatBoxSession($chatbox);
        }
    }

    if ($items != '') {
        $items = substr($items, 0, -1);
    }

header('Content-type: application/json');
?>
{
        "username": "<?php echo $_SESSION['username'];?>",
        "items": [
            <?php echo $items;?>
        ]
}

<?php

    exit(0);
}

function sendChat() {
    $from = $_SESSION['username'];
    $to = $_POST['to'];
    $message = $_POST['message'];

    $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());

    $messagesan = sanitize($message);

    if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
        $_SESSION['chatHistory'][$_POST['to']] = '';
    }

    $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
                       {
            "s": "1",
            "f": "{$to}",
            "m": "{$messagesan}"
       },
EOD;

    unset($_SESSION['tsChatBoxes'][$_POST['to']]);

    $sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())";
    $query = mysql_query($sql);
    echo "1";
    exit(0);
}

function closeChat() {

    unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);

    echo "1";
    exit(0);
}

function sanitize($text) {
    $text = htmlspecialchars($text, ENT_QUOTES);
    $text = str_replace("\n\r","\n",$text);
    $text = str_replace("\r\n","\n",$text);
    $text = str_replace("\n","<br>",$text);
    return $text;
}

Which is showed little difficult for me/ Can you edit this code for me? regards

i have edited according the tutorial . Getting error
[21-Jul-2020 15:37:51 UTC] PHP Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/xxxxx/public_html/chat/chat.php on line 183
[21-Jul-2020 15:37:51 UTC] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/xxxxxi/public_html/chat/chat.php on line 184
[21-Jul-2020 15:37:51 UTC] PHP Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/xxxxx/public_html/chat/chat.php on line 183

Can you provide the updated code because, for example, the error log says that mysql_query() is invalid on line 183, but in the code you provided, there is no mysql_query() function being called on line 183.

However, I do see that you have on line 179 above $query = mysql_query($sql); but you are not specifying the MySQL connection ID in the mysql_query() function. In other words, you're passing in some SQL string without specifying which database connection you want to use to execute this query.

Try using mysqli_query($dbh, $sql) instead.

Thanks for your reply . This is my orginal site code which i used and updated

<?php
$host = "localhost";
$user = "user";
$password = "password";
$database = "database";
$conn = mysqli_connect($host, $user, $password, $database);
if(!$conn){
    echo ('unable to connect to database');
}
session_start();
if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); } 
if ($_GET['action'] == "sendchat") { sendChat(); } 
if ($_GET['action'] == "closechat") { closeChat(); } 
if ($_GET['action'] == "startchatsession") { startChatSession(); } 
if ($_GET['action'] == "chatname") { chatName(); } 

if (!isset($_SESSION['chatHistory'])) {
    $_SESSION['chatHistory'] = array(); 
}

if (!isset($_SESSION['openChatBoxes'])) {
    $_SESSION['openChatBoxes'] = array();   
}
function chatHeartbeat($conn)
{

    $sql = "select register.username,register.gender,register.photo1,chat.from,chat.message,chat.to,chat.id,chat.sent,chat.recd from chat,register where (chat.to = '".mysqli_real_escape_string($conn, $_SESSION['chatuser'])."' AND recd = 0) and chat.from=register.index_id order by id ASC";

    $query=mysqli_query($conn, $sql);
    $items = '';

    $chatBoxes = array();

    while ($chat = mysqli_fetch_array($conn, $query)) {

        if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
            $items = $_SESSION['chatHistory'][$chat['from']];
        }
        $chat['username'] =sanitize( $chat['username']);
        $chat['message'] = sanitize($chat['message']);

        if($chat['photo1']=='')
        {
             if($chat['gender']=='Male')
             {
             $chat['photo1']="male_small.png";
             }      
             else
             {
             $chat['photo1']= "female_small.png";
             }          
        }

        $items .= <<<EOD
                       {
            "s": "0",
            "u": "{$chat['username']}",
            "ph": "{$chat['photo1']}",
            "f": "{$chat['from']}",
            "m": "{$chat['message']}"
       },
EOD;

    if (!isset($_SESSION['chatHistory'][$chat['from']])) {
        $_SESSION['chatHistory'][$chat['from']] = '';
    }

    $_SESSION['chatHistory'][$chat['from']] .= <<<EOD
                           {
            "s": "0",
            "u": "{$chat['username']}",
            "f": "{$chat['from']}",
            "ph": "{$chat['photo1']}",
            "m": "{$chat['message']}"
       },
EOD;

        unset($_SESSION['tsChatBoxes'][$chat['from']]);
        $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
    }

    if (!empty($_SESSION['openChatBoxes'])) {
    foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
        if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
            $now = time()-strtotime($time);
            $time = date('g:iA M dS', strtotime($time));

            $message = "Sent at $time";
            if ($now > 180) {
                $items .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;

    if (!isset($_SESSION['chatHistory'][$chatbox])) {
        $_SESSION['chatHistory'][$chatbox] = '';
    }

    $_SESSION['chatHistory'][$chatbox] .= <<<EOD
        {
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
            $_SESSION['tsChatBoxes'][$chatbox] = 1;
        }
        }
    }
}

    $sql = "update chat set recd = 1 where chat.to = '".mysqli_real_escape_string($conn, $_SESSION['chatuser'])."' and recd = 0";
    $query = mysqli_query($conn, $sql);

    if ($items != '') {
        $items = substr($items, 0, -1);
    }
header('Content-type: application/json');
?>
{
        "items": [
            <?php echo $items;?>
        ]
}

<?php
            exit(0);
}

function chatBoxSession($chatbox) {

    $items = '';

    if (isset($_SESSION['chatHistory'][$chatbox])) {
        $items = $_SESSION['chatHistory'][$chatbox];
    }

    return $items;
}

function startChatSession() {
    $items = '';
    if (!empty($_SESSION['openChatBoxes'])) {
        foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
            $items .= chatBoxSession($chatbox);
        }
    }

    if ($items != '') {
        $items = substr($items, 0, -1);
    }

header('Content-type: application/json');
/*
$suser=$_SESSION['chatuser'];
$sc=mysql_query("select username from register where username='$su'");
while($row_sc=mysql_fetch_array($sc))
{
$_SESSION['current_chat_username']=$row_sc['username'];
}*/
?>
{
        "username": "<?php echo $_SESSION['chatuser'];?>",
        "items": [
            <?php echo $items;?>
        ]
}

<?php
    exit(0);

}

function chatName() {
    $un = '';

$su=$_GET['usw'];
$sc2=mysqli_query("select username from register where index_id='$su' limit 1");
while($row_sc2=mysqli_fetch_array($conn, $sc2))
{
$un=$row_sc2["username"];
}
?>
{
        "unm": ["<?php echo $un;?>"]

}

<?php

    exit(0);
}

function sendChat() {
    $from = $_SESSION['chatuser'];
    $to = $_POST['to'];
    $message = $_POST['message'];
    $sql = "select register.username,register.photo1 from register where register.index_id='$from' limit 1";
    $uname = mysqli_query($conn, $sql);
    $from_user='';
    while ($un = mysqli_fetch_array($conn, $uname)) {
    $from_user=$un['username'];
    $user_photo=$un['photo1'];
    }

    $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());

    $messagesan = sanitize($message);

    if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
        $_SESSION['chatHistory'][$_POST['to']] = '';
    }

    $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
                       {
            "s": "1",
            "u": "{$from_user}",
            "f": "{$to}",
            "m": "{$messagesan}",
            "ph": "{$user_photo}"
       },
EOD;

    unset($_SESSION['tsChatBoxes'][$_POST['to']]);

    $sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysqli_real_escape_string($conn, $from)."', '".mysqli_real_escape_string($conn, $to)."','".mysqli_real_escape_string($conn, $message)."',NOW())";
    $query = mysqli_query($conn, $sql);
    echo "1";
    exit(0);
}

function closeChat() {

    unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);

    echo "1";
    exit(0);
}

function sanitize($text) {
    $text = htmlspecialchars($text, ENT_QUOTES);
    $text = str_replace("\n\r","\n",$text);
    $text = str_replace("\r\n","\n",$text);
    $text = str_replace("\n","<br>",$text);
    return $text;
}
?>

This code old one with mysql function . This code perfectly working with mysql

<?php
session_start();
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'bis');
define('DB_PASSWORD', 'ammaamma');
define('DB_DATABASE', 'bismatrimony');

$connection = @mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$selectdb = @mysql_select_db(DB_DATABASE);

if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); } 
if ($_GET['action'] == "sendchat") { sendChat(); } 
if ($_GET['action'] == "closechat") { closeChat(); } 
if ($_GET['action'] == "startchatsession") { startChatSession(); } 
if ($_GET['action'] == "chatname") { chatName(); } 

if (!isset($_SESSION['chatHistory'])) {
    $_SESSION['chatHistory'] = array(); 
}

if (!isset($_SESSION['openChatBoxes'])) {
    $_SESSION['openChatBoxes'] = array();   
}

function chatHeartbeat() {
    $sql = "select register.username,register.gender,register.photo1,chat.from,chat.message,chat.to,chat.id,chat.sent,chat.recd from chat,register where (chat.to = '".mysql_real_escape_string($_SESSION['chatuser'])."' AND recd = 0) and chat.from=register.index_id order by id ASC";

    $query = mysql_query($sql);
    $items = '';

    $chatBoxes = array();

    while ($chat = mysql_fetch_array($query)) {

        if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
            $items = $_SESSION['chatHistory'][$chat['from']];
        }
        $chat['username'] =sanitize( $chat['username']);
        $chat['message'] = sanitize($chat['message']);

        if($chat['photo1']=='')
        {
             if($chat['gender']=='Male')
             {
             $chat['photo1']="male_small.png";
             }      
             else
             {
             $chat['photo1']= "female_small.png";
             }          
        }

        $items .= <<<EOD
                       {
            "s": "0",
            "u": "{$chat['username']}",
            "ph": "{$chat['photo1']}",
            "f": "{$chat['from']}",
            "m": "{$chat['message']}"
       },
EOD;

    if (!isset($_SESSION['chatHistory'][$chat['from']])) {
        $_SESSION['chatHistory'][$chat['from']] = '';
    }

    $_SESSION['chatHistory'][$chat['from']] .= <<<EOD
                           {
            "s": "0",
            "u": "{$chat['username']}",
            "f": "{$chat['from']}",
            "ph": "{$chat['photo1']}",
            "m": "{$chat['message']}"
       },
EOD;

        unset($_SESSION['tsChatBoxes'][$chat['from']]);
        $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
    }

    if (!empty($_SESSION['openChatBoxes'])) {
    foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
        if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
            $now = time()-strtotime($time);
            $time = date('g:iA M dS', strtotime($time));

            $message = "Sent at $time";
            if ($now > 180) {
                $items .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;

    if (!isset($_SESSION['chatHistory'][$chatbox])) {
        $_SESSION['chatHistory'][$chatbox] = '';
    }

    $_SESSION['chatHistory'][$chatbox] .= <<<EOD
        {
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
            $_SESSION['tsChatBoxes'][$chatbox] = 1;
        }
        }
    }
}

    $sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['chatuser'])."' and recd = 0";
    $query = mysql_query($sql);

    if ($items != '') {
        $items = substr($items, 0, -1);
    }
header('Content-type: application/json');
?>
{
        "items": [
            <?php echo $items;?>
        ]
}

<?php
            exit(0);
}

function chatBoxSession($chatbox) {

    $items = '';

    if (isset($_SESSION['chatHistory'][$chatbox])) {
        $items = $_SESSION['chatHistory'][$chatbox];
    }

    return $items;
}

function startChatSession() {
    $items = '';
    if (!empty($_SESSION['openChatBoxes'])) {
        foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
            $items .= chatBoxSession($chatbox);
        }
    }

    if ($items != '') {
        $items = substr($items, 0, -1);
    }

header('Content-type: application/json');
/*
$suser=$_SESSION['chatuser'];
$sc=mysql_query("select username from register where username='$su'");
while($row_sc=mysql_fetch_array($sc))
{
$_SESSION['current_chat_username']=$row_sc['username'];
}*/
?>
{
        "username": "<?php echo $_SESSION['chatuser'];?>",
        "items": [
            <?php echo $items;?>
        ]
}

<?php
    exit(0);

}

function chatName() {
    $un = '';

$su=$_GET['usw'];
$sc2=mysql_query("select username from register where index_id='$su' limit 1");
while($row_sc2=mysql_fetch_array($sc2))
{
$un=$row_sc2["username"];
}
?>
{
        "unm": ["<?php echo $un;?>"]

}

<?php

    exit(0);
}

function sendChat() {
    $from = $_SESSION['chatuser'];
    $to = $_POST['to'];
    $message = $_POST['message'];
    $sql = "select register.username,register.photo1 from register where register.index_id='$from' limit 1";
    $uname = mysql_query($sql);
    $from_user='';
    while ($un = mysql_fetch_array($uname)) {
    $from_user=$un['username'];
    $user_photo=$un['photo1'];
    }

    $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());

    $messagesan = sanitize($message);

    if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
        $_SESSION['chatHistory'][$_POST['to']] = '';
    }

    $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
                       {
            "s": "1",
            "u": "{$from_user}",
            "f": "{$to}",
            "m": "{$messagesan}",
            "ph": "{$user_photo}"
       },
EOD;

    unset($_SESSION['tsChatBoxes'][$_POST['to']]);

    $sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())";
    $query = mysql_query($sql);
    echo "1";
    exit(0);
}

function closeChat() {

    unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);

    echo "1";
    exit(0);
}

function sanitize($text) {
    $text = htmlspecialchars($text, ENT_QUOTES);
    $text = str_replace("\n\r","\n",$text);
    $text = str_replace("\r\n","\n",$text);
    $text = str_replace("\n","<br>",$text);
    return $text;
}
?>

So what error messages are you receiving with the updated code?

Line 183 doesn't have a mysqli_query() call.

[21-Jul-2020 15:37:51 UTC] PHP Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/xxxxx/public_html/chat/chat.php on line 183
[21-Jul-2020 15:37:51 UTC] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/xxxxxi/public_html/chat/chat.php on line 184

After i have converted mysqli but the same error is coming

<?php
session_start();
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');

$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysqli_error($connection));

$selectdb= mysqli_select_db($connection,DB_DATABASE);

if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); } 
if ($_GET['action'] == "sendchat") { sendChat(); } 
if ($_GET['action'] == "closechat") { closeChat(); } 
if ($_GET['action'] == "startchatsession") { startChatSession(); } 
if ($_GET['action'] == "chatname") { chatName(); } 

if (!isset($_SESSION['chatHistory'])) {
    $_SESSION['chatHistory'] = array(); 
}

if (!isset($_SESSION['openChatBoxes'])) {
    $_SESSION['openChatBoxes'] = array();   
}

function chatHeartbeat() {
    $sql = "select register.username,register.gender,register.photo1,chat.from,chat.message,chat.to,chat.id,chat.sent,chat.recd from chat,register where (chat.to = '".mysqli_real_escape_string($connection,$_SESSION['chatuser'])."' AND recd = 0) and chat.from=register.index_id order by id ASC";

    $query = mysqli_query($connection,$sql);
    $items = '';

    $chatBoxes = array();

    while ($chat = mysqli_fetch_array($query)) {

        if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
            $items = $_SESSION['chatHistory'][$chat['from']];
        }
        $chat['username'] =sanitize( $chat['username']);
        $chat['message'] = sanitize($chat['message']);

        if($chat['photo1']=='')
        {
             if($chat['gender']=='Male')
             {
             $chat['photo1']="male_small.png";
             }      
             else
             {
             $chat['photo1']= "female_small.png";
             }          
        }

        $items .= <<<EOD
                       {
            "s": "0",
            "u": "{$chat['username']}",
            "ph": "{$chat['photo1']}",
            "f": "{$chat['from']}",
            "m": "{$chat['message']}"
       },
EOD;

    if (!isset($_SESSION['chatHistory'][$chat['from']])) {
        $_SESSION['chatHistory'][$chat['from']] = '';
    }

    $_SESSION['chatHistory'][$chat['from']] .= <<<EOD
                           {
            "s": "0",
            "u": "{$chat['username']}",
            "f": "{$chat['from']}",
            "ph": "{$chat['photo1']}",
            "m": "{$chat['message']}"
       },
EOD;

        unset($_SESSION['tsChatBoxes'][$chat['from']]);
        $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
    }

    if (!empty($_SESSION['openChatBoxes'])) {
    foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
        if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
            $now = time()-strtotime($time);
            $time = date('g:iA M dS', strtotime($time));

            $message = "Sent at $time";
            if ($now > 180) {
                $items .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;

    if (!isset($_SESSION['chatHistory'][$chatbox])) {
        $_SESSION['chatHistory'][$chatbox] = '';
    }

    $_SESSION['chatHistory'][$chatbox] .= <<<EOD
        {
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
            $_SESSION['tsChatBoxes'][$chatbox] = 1;
        }
        }
    }
}

    $sql = "update chat set recd = 1 where chat.to = '".mysqli_real_escape_string($connection,$_SESSION['chatuser'])."' and recd = 0";
    $query = mysqli_query($connection,$sql);

    if ($items != '') {
        $items = substr($items, 0, -1);
    }
header('Content-type: application/json');
?>
{
        "items": [
            <?php echo $items;?>
        ]
}

<?php
            exit(0);
}

function chatBoxSession($chatbox) {

    $items = '';

    if (isset($_SESSION['chatHistory'][$chatbox])) {
        $items = $_SESSION['chatHistory'][$chatbox];
    }

    return $items;
}

function startChatSession() {
    $items = '';
    if (!empty($_SESSION['openChatBoxes'])) {
        foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
            $items .= chatBoxSession($chatbox);
        }
    }

    if ($items != '') {
        $items = substr($items, 0, -1);
    }

header('Content-type: application/json');
/*
$suser=$_SESSION['chatuser'];
$sc=mysqli_query($connection,"select username from register where username='$su'");
while($row_sc=mysqli_fetch_array($sc))
{
$_SESSION['current_chat_username']=$row_sc['username'];
}*/
?>
{
        "username": "<?php echo $_SESSION['chatuser'];?>",
        "items": [
            <?php echo $items;?>
        ]
}

<?php
    exit(0);

}

function chatName() {
    $un = '';

$su=$_GET['usw'];
$sc2=mysqli_query($connection,"select username from register where index_id='$su' limit 1");
while($row_sc2=mysqli_fetch_array($sc2))
{
$un=$row_sc2["username"];
}
?>
{
        "unm": ["<?php echo $un;?>"]

}

<?php

    exit(0);
}

function sendChat() {
    $from = $_SESSION['chatuser'];
    $to = $_POST['to'];
    $message = $_POST['message'];
    $sql = "select register.username,register.photo1 from register where register.index_id='$from' limit 1";
    $uname = mysqli_query($connection,$sql);
    $from_user='';
    while ($un = mysqli_fetch_array($uname)) {
    $from_user=$un['username'];
    $user_photo=$un['photo1'];
    }

    $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());

    $messagesan = sanitize($message);

    if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
        $_SESSION['chatHistory'][$_POST['to']] = '';
    }

    $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
                       {
            "s": "1",
            "u": "{$from_user}",
            "f": "{$to}",
            "m": "{$messagesan}",
            "ph": "{$user_photo}"
       },
EOD;

    unset($_SESSION['tsChatBoxes'][$_POST['to']]);

    $sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysqli_real_escape_string($connection,$from)."', '".mysqli_real_escape_string($connection,$to)."','".mysqli_real_escape_string($connection,$message)."',NOW())";
    $query = mysqli_query($connection,$sql);
    echo "1";
    exit(0);
}

function closeChat() {

    unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);

    echo "1";
    exit(0);
}

function sanitize($text) {
    $text = htmlspecialchars($text, ENT_QUOTES);
    $text = str_replace("\n\r","\n",$text);
    $text = str_replace("\r\n","\n",$text);
    $text = str_replace("\n","<br>",$text);
    return $text;
}
?>

I am getting this type of error

[23-Jul-2020 06:56:51 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /home/username/public_html/chat/chat.php on line 25

That means that, at that point of your script, the value of $connection is NULL for some reason.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.