我的MySQL查询有什么问题?

时间:2015-03-03 10:07:09

标签: mysql database-connection sql-insert mysql-error-1064

我的代码中有一个警告:

<?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 个答案:

答案 0 :(得分:2)

当你连接到数据库时,你没有设置实例变量,你只是设置一个局部变量。它必须是:

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