2015-03-03 106 views
1

我在我的代碼,一個警告:我的MySQL查詢有什麼問題?

<?php 

class db 
{ 

    public $db; 
    function __constract() 
    { 
     $db= mysql_connect("127.0.0.1","root","") or die("Can not to connect the server"); 
     mysql_query("SET NAMES 'utf8'" , $this->db) or die("Can not to connect the server"); 
     mysql_select_db("test" , $this->db) or die("Can not to connect the server"); 

    } 
    function register($user,$pass,$email,$gender,$city) 
    { 
    $q="INSERT INTO `tbl_users`() VALUES (NULL , '$user' , '$pass' , '$email' , '$gender' , '$city')"; 
    mysql_query($q,$this->db); 
    } 
} 
?> 

當發送查詢我有以下的警告並沒有在表中插入:

Warning: mysql_query() expects parameter 2 to be resource, null given in C:\UwAmp\www\amin\include\config.php on line 17 
+1

變化'INSERT INTO tbl_users()VALUES'爲'INSERT INTO tbl_users VALUES' – 2015-03-03 10:08:13

+0

不要使用'mysql_ *'API。這些被刪除。使用'mysqli_ *'或者pdo和準備好的語句。 – Jens 2015-03-03 10:09:35

回答

2

當你連接你不設置實例變量到數據庫,你只需設置一個局部變量。它需要:

$this->db= mysql_connect("127.0.0.1","root","") or die("Can not to connect the server");