1

我正在尝试从 to 移动MySQLMySQLi但遇到了一些问题。我的SQLi连接开始如下所示:

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

我正在尝试使用它MySQLi来存储我的会话数据,但是,我的 _write 构造上不断出现错误(它未完成,像这样发布,因为错误发生在第 56 行,带有 的那个$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')");
    }

给出的错误是:(Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given in [...]settings.php on line 56该行是 $stmt=...)

谁能告诉我我的问题可能出在哪里?

编辑 Var 转储外部函数,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) "正常运行时间:373 线程:1 问题:5慢查询:0 打开:34 刷新表:1 打开表:27 每秒查询平均:0.013" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(5) ["warning_count"]=> int(0) }

函数内部的var转储,global $langCon; var_dump($langCon);:NULL。

4

1 回答 1

1

来自手册;尝试这样的事情(它必须看起来像 $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;
}
于 2012-06-19T08:17:17.557 回答