1

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.

8
  • Try using it without $langCon at the beginning of the prepare? Commented Jun 19, 2012 at 8:12
  • Is your connection code within a function? Have you declared $langCon global there as well? Commented Jun 19, 2012 at 8:13
  • @Bono Warning: mysqli_prepare() expects exactly 2 parameters, 1 given Commented Jun 19, 2012 at 8:13
  • @lanzz: my connection code isn't within a function. It is in the same file as _write($id, $data). Commented Jun 19, 2012 at 8:15
  • 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. Commented Jun 19, 2012 at 8:17

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

11 Comments

Both my procedural style and your OO style work outside functions, however, when I try the OO style inside _write() I get Fatal error: Call to a member function prepare() on a non-object
@Limoncello And $mysqli is set in that code? AKA you've got acces to it (I see you put $langCon as global, is $mysqli global?)
Yes, my _write() function begins with global $langCon, $mysqli;
@Limoncello Have you tried mysqli_prepare($mysqli, 'SQL'); ? Since it's asking for mysqli(). Also have you got all error reporting on?
Then I get Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given . That's the reason of my frustration here, I did everything according to the manual, declared everything that needs to be global as global, and still get errors.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.