2012-06-19 66 views
1

我想從MySQL移動到MySQLi,但我有一些問題。我SQLi連接開始,如下圖所示:MySQLi在功能上發揮作用

$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(該行是$語句= ...)

任何人都可以請我把我關在我的問題可能是哪裏?

編輯外功能瓦爾轉儲,var_dump($langCon):內部功能,global $langCon; 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) }

瓦爾轉儲:NULL。

+0

嘗試使用它沒有$ langCon在準備的開始? – Bono

+0

您的連接代碼是否在一個函數中?你有沒有在全球範圍聲明'$ langCon'? – lanzz

+0

@Bono警告:mysqli_prepare()需要2個參數,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; 
} 
+0

我的程序風格和OO風格都在函數外部工作,但是,當我在_write()內部嘗試OO風格時,我得到了'致命錯誤:調用成員函數prepare()在非對象上' –

+0

@Limoncello And $ mysqli是在該代碼中設置的? AKA你有它的存取權限(我看到你把$ langCon作爲全局的,是$ mysqli全球?) – Bono

+0

是的,我的_write()函數以'global $ langCon,$ mysqli;' –