0
public function getProfile($user_id)
{
    $stmt = $this->_connection->prepare("SELECT * FROM Profile WHERE profile_id = :profile_id");
    $stmt->execute(['profile_id'=>$user_id]);
    $stmt->setFetchMode(PDO::FETCH_CLASS, "Profile_model"); //datatype user
    return $stmt->fetch(); //it should return a user
}

Hi as I wrote my code here. When we prepare for PDO, it is prepare("SELECT * FROM Profile WHERE profile_id = :profile_id"); and why is there : in front of profile_id?

and $stmt->execute(['profile_id'=>$user_id]); so as far as I know, => is like definition so the left side would be a key and the right side would be a value so does that mean that profile_id variable will have a value of $user_id?

Udhav Sarvaiya
  • 6,939
  • 10
  • 42
  • 51
  • 4
    The `:profile` is a named placeholder. It's not part of the PHP language itself, but it is part of the PDO database library. Those placeholders then are used in prepared statements. You can read more [here](https://phpdelusions.net/pdo#prepared). – solarc Mar 29 '19 at 04:10
  • `=>`is assigning the key a value. `profile_id` is the key and `$user_id` is its value. The PDO driver maps that to the corresponding placeholder. – user3783243 Mar 29 '19 at 04:15

1 Answers1

1
$stmt = $this->_connection->prepare("SELECT * FROM Profile WHERE profile_id = :profile_id");

As this is still an unprepared statement, while :profile_id is a placeholder (you can think of something like variable), the query look like this in this stage:

SELECT * FROM Profile WHERE profile_id = (YOUR PROFILE ID HERE)

However, when u run $stmt->execute(['profile_id'=>$user_id]); it assigns the value of $user_id into the placeholder :profile_id. Let's say the $user_id is 3, the query will looks like this:

SELECT * FROM Profile WHERE profile_id = 3

The => operator is to assign value in to a key when you are using an associative array, so you are right.

Seah Sky
  • 136
  • 9