1
votes

I am trying to move from MySQLtoMySQLibut am having some problems. MySQLiconnection starts as shown below:

$langCon=mysqli_connect('localhost', 'root', '', 'english');
if (!$langCon) die('Couldn\'t connect: '.mysqli_error());

and I am trying to useMySQLito store my session data, however, I keep getting an error on my _write construct (It is unfinished, posting like this since the error occurs on line 56, the one with$stmt=...):

function _write($id, $data){ /
        global $langCon;
        $stmt=mysqli_prepare($langCon,'REPLACE INTO `sess`(`id`,`access`,`data`) VALUES(?,?,?)');
        $id = mysqli_real_escape_string($langCon,$id);
        $data = mysqli_real_escape_string($langCon,$data);
        $t=$_SERVER['REQUEST_TIME'];
        return mysqli_query($langCon,"REPLACE INTO `sess`(`id`,`access`,`data`) VALUES('$id','$t','$data')");
    }

The error given is: Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given in [...]settings.php on line 56 (the line is $stmt=...)

Could anyone please tip me off on where my problem might be?

EDIT Var dump outside function,var_dump($langCon):

object(mysqli)#2 (18) { ["affected_rows"]=> int(0) ["client_info"]=> string(50) "mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $" ["client_version"]=> int(50008) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["field_count"]=> int(0) ["host_info"]=> string(20) "localhost via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(10) "5.5.21-log" ["server_version"]=> int(50521) ["stat"]=> string(130) "Uptime: 373 Threads: 1 Questions: 5 Slow queries: 0 Opens: 34 Flush tables: 1 Open tables: 27 Queries per second avg: 0.013" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(5) ["warning_count"]=> int(0) }

Var dump inside function, global $langCon; var_dump($langCon);: NULL.

1
Try using it without $langCon at the beginning of the prepare?Bono
Is your connection code within a function? Have you declared $langCon global there as well?lanzz
@Bono Warning: mysqli_prepare() expects exactly 2 parameters, 1 givenAM-
@lanzz: my connection code isn't within a function. It is in the same file as _write($id, $data).AM-
And you are certain you're calling _write() after you have connected? Because there is no way for us to know that from the code you posted.lanzz

1 Answers

1
votes

From the manual; try something like this (it has to look like $var->prepare)

$mysqli = new mysqli("localhost", "root", "", "english");
if ($mysqli->connect_errno) 
{
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) 
{
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}