MySQLi在函数中表现出来

时间:2012-06-19 08:08:34

标签: php mysqli

我正试图从MySQL转到MySQLi,但遇到了一些问题。我的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)“”   [ “场计数”] => 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。

1 个答案:

答案 0 :(得分: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;
}