0

The bind parameter function, bind_param() is considered a non object and keeps throwing errors that hurt my head as i'm new to the PHP thing. The code used to connect to the database was originally throwing errors, even though I copied it from my login script where it was working...

<?php
class User {
    private $result;
    private static $db;

    public function __construct($cookie) {
        self::$db = mysqli_connect('localhost', 'username', '', 'dbname');
        var_dump(self::$db);
        
        $query = self::$db->prepare('SELECT * FROM users WHERE cookie=?');
        var_dump($query);
        $query->bind_param('s', $cookie);
        

        $this->result = $query->get_result()->fetch_array();
    }
    
    public static function checkUser($cookie) {
        self::$db = mysqli_connect('localhost', 'username', "", 'dbname');
        
        $query = self::$db->prepare('SELECT cookie FROM users WHERE cookie=?');
        $query->bind_param('s', $cookie);

        $result = $query->get_result()->fetch_array();
        
        if ($cookie == $result[0]) return true;

        return false;
    }

    public function getName() {
        return $this->result['name'];
    }

    public function getSurname() {
        return $this->result['surname'];
    }

    public function getEmail() {
        return $this->result['email'];
    }

    public function getGroup() {
        return $this->result['perm_group'];
    }

    public function getEstablishment() {
        return $this->result['establishment'];
    }
}
?>

The idea was to to make a class (User) so that we can call functions from other pages to get details of the users, such as Cookie. I've been doing this for hours and I honestly don't know whats going on.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Isaac Manzi
  • 36
  • 1
  • 9
  • what does `var_dump(self::$db);` giving? – Sahil Gulati May 26 '17 at 10:50
  • probably connection problems – Rotimi May 26 '17 at 10:51
  • object(mysqli)[2] public 'affected_rows' => int 0 public 'client_info' => string 'mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $' (length=79) public 'client_version' => int 50011 public 'connect_errno' => int 0 public 'connect_error' => null public 'errno' => int 0 public 'error' => string '' (length=0) public 'error_list' => array (size=0) empty public 'field_count' => int 0 public 'host_info' => string 'Localhost via UNIX socket' (length=25) public 'info' => null public 'insert_id' => int 0 – Isaac Manzi May 26 '17 at 10:52
  • No connection problems, we connect successfully – Isaac Manzi May 26 '17 at 10:53
  • in your checkUser function, why are you establishing connection again? Doesn't the `self:db` have the connection from `construct`? – Rotimi May 26 '17 at 10:54
  • I'm not calling the constructor, so the self:db would be empty/undefined otherwise. – Isaac Manzi May 26 '17 at 10:56
  • You don't call contrusctor. That is the first function to be called in a class. It calls itself. – Rotimi May 26 '17 at 10:57
  • Won't it only call itself if creating a new User, which we don't do? – Isaac Manzi May 26 '17 at 10:58
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Jul 20 '20 at 22:49

2 Answers2

0

mysqli::prepare returns false, if an error occurred.

$query = self::$db->prepare('SELECT cookie FROM users WHERE cookie=?');
$query->bind_param('s', $cookie);

If there is an error, you have no result-object to call bind_param on.

0

The issue was real simple, we forgot to have a column to store cookies.

Sorry for the stupid question ladies

Isaac Manzi
  • 36
  • 1
  • 9