0

hi im attempting to update a script and i have come across the following error PHP Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in so here was the orginal

$sql = "select * from chat where (chat.to = '".mysqli_real_escape_string($_SESSION['usernamechat'])."' AND recd = 0) order by id ASC";

of which i changed to

$sql = "select * from chat where (chat.to = '".mysqli_real_escape_string($conn, $_SESSION['usernamechat'])."' AND recd = 0) order by id ASC"; 

and still getting the same error any ideas anyone

ty in advance jan x

3
Contributors
3
Replies
22
Views
11 Months
Discussion Span
Last Post by diafol
2

Hi,

right before this code, set a check:

/* check connection */
if(mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

Too see why the connection returns null.

0

$conn was undefined so added global $conn; into function and it solved it ty very much hun x

0

An alternative to making $conn global would be to pass the $conn as a parameter for your function. Globals are not always the best idea - see this v. old article: http://stackoverflow.com/a/5166527

$conn = ...(whatever)... ;
$usernameChat = $_SESSION['usernamechat'];

$chatLines = getChat( $conn, $usernameChat );

function getChat( $connection, $username ){
    $user = mysqli_real_escape_string( $connection, $username );
    $sql = "SELECT *  FROM `chat` WHERE `to` = '$user' AND `recd` = 0 ORDER BY `id`"; 
    //run and extract and modify and generally fiddle about
    return $data;
}

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.