-1

I am just learning classes , i am trying to add data using mysql bind params but unable to do so , seems some silly mistake , please help me with is , i want to do it with oops way , else i can do it.

Error : Parse error: syntax error, unexpected '$mysqli' (T_VARIABLE), expecting function (T_FUNCTION) in customer.php on line 4

Index.php

$adduser= new Userclass();
echo $adduser->addcutomer();

Db class.php

class db extends mysqli {
    private static $instance = null;
    private $user = "root";
    private $pass = "xxx";
    private $dbName = "xxx";
    private $dbHost = "localhost";

    public static function getInstance() {
    if (!self::$instance instanceof self) {
            self::$instance = new self;
    }
        return self::$instance;
    }
    public function __clone() {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
    public function __wakeup() {
        trigger_error('Deserializing is not allowed.', E_USER_ERROR);
    }

    private function __construct() {
        parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
        if (mysqli_connect_error()) {
            exit('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
        parent::set_charset('utf-8');
   }

    public function dbquery($query)
    {
        if($this->query($query))
        {
            return true;
        }

    }
}

Customer Class

class Userclass extends db{
  $mysqli=db::getInstance();

  public function addcutomer(){

    $query = $mysqli->prepare("INSERT INTO `user` (`firstname`,`lastname`) VALUES (?, ?)");
    $col1 = 'abc';
    $col2 = 'def';
    $query->bind_param('ss', $col1, $col2);
    $query->execute();
    $query->close();
    return true;
  }
gaurav
  • 237
  • 4
  • 16
  • pass $col1 = abc; $col2 = def; this in string like $col1 = 'abc'; $col2 = 'def'; – Manthan Dave Nov 02 '16 at 12:06
  • You need to check the manual on how to set and access object properties. Also note that it is highly unlikely that you want all your classes to extend `mysqli`. You should take a look at dependency injection instead. – jeroen Nov 02 '16 at 12:18
  • @jeroen , please help me with above code , seems some tweaking needed in db.php or customerclass.php – gaurav Nov 02 '16 at 12:27

1 Answers1

-1

Try this, hope this work

$col1 = "abc";
$col2 = "def";
Waleed Ahmed Haris
  • 1,229
  • 11
  • 17