2015-03-03 60 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");