amin samadazadeh
amin samadazadeh

Reputation: 11

what's wrong in my MySQL query?

I have a warning in my code:

<?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);
    }
}
?>

When send query I have the following warning and not insert in the table:

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

Upvotes: 1

Views: 170

Answers (1)

Barmar
Barmar

Reputation: 780688

You're not setting the instance variable when you connect to the database, you're just setting a local variable. It needs to be:

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

Upvotes: 2

Related Questions