-2

I started using mysqli_* functions instead of the old mysql_* functions in PHP, and I'm having a bit of a problem with this code:

public function addUser($table, $code = false, $rows = false) {
   if (!$code) {
      header("Location: " . $this->authenticate());
   } else {
      $this->getToken($code);
   }
   $user = $this->getEndpoint('users/current', false, true);
   $user = $user->response->user;
   if (!$rows)
        $rows = array(
            "remote_id" => $user->id,
            "firstName" => $user->first_name,
            "lastName" => $user->last_name,
            "photo" => $user->photo->medium,
            "gender" => $user->gender == 'male' ? 1 : 2,
            "email" => $user->contact->email,
        );
   $rows['access_token'] = $this->accessToken;
   $stmt = $this->mysql->prepare("SELECT id FROM users WHERE access_token = '{$this->accessToken}'"); //line 136
   $stmt->execute(); //line 137
}

The code returns these 2 errors:

Warning: mysqli::prepare(): Couldn't fetch MySQL in C:\Users\Grega\Server\application\inc\classes\APIConnect.php on line 136

Fatal error: Call to a member function execute() on a non-object in C:\Users\Grega\Server\application\inc\classes\APIConnect.php on line 137

What is the reason for 'Couldn't fetch MySQL'? The database connection is correct, it works in other classes, and the query returns a valid result, if I echo it and execute it in phpMyAdmin. Also, my variable is named mysql NOT mysqli!

Community
  • 1
  • 1
Gregor Menih
  • 5,036
  • 14
  • 44
  • 66

2 Answers2

0

You should read more about the difference between MYSQL to MYSQLi.

While your code is:

$stmt = $this->mysql->prepare("SELECT id FROM users WHERE access_token = '{$this->accessToken}'");

You should do it like this:

$stmt = $this->mysql->prepare("SELECT id FROM users WHERE access_token = ?");
$stmt->bind_param("s" , $this->accessToken ); //Used 's' as I guess that the accessToken is a string

The binding part is the critical part of the prepare thing. (Your queries are safe)

After that you can use $stmt->execute(); and get_result().

Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39
  • Please add about named bindings too, like `:access_token` instead of `?` to cover all areas. :) – saji89 Oct 13 '12 at 13:22
  • 3
    As far as I know , mysqli doesn't support in named binding , unlike PDO. – Ofir Baruch Oct 13 '12 at 13:31
  • Thanks for explaining the ::prepare method, but this doesn't fix anything. I still get the same error, also using any other method will return the same error. – Gregor Menih Oct 13 '12 at 13:44
  • @OfirBaruch, Oh sorry. My mistake. You're right... [PDO Vs Mysql](http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/) – saji89 Oct 14 '12 at 04:14
  • @GregaMenih , can you edit your question and paste the new code? – Ofir Baruch Oct 14 '12 at 16:15
  • @OfirBaruch can *you* edit your answer that has **absolutely nothing to do with the question asked**, specifically the error message "Couldn't fetch mysqli"? – Your Common Sense Jun 07 '22 at 07:54
0

For the first error: you probably closed the connection somewhere before this code, and this is why ($stmt -> close() )

For the second error: when you use (prepare()), you first introduce a SQL template to the database, which then has to pass the parameters to the "bind_param()" method to send it to the database with another protocol (to prevent SQL injection) and reduce the request and attach the parameters to SQL with execute() method