Reputation: 341
function get_actor_info( $actor_id ) {
global $pdo;
$stmt = $pdo->prepare('
SELECT film_info, first_name, last_name
FROM actor_info
WHERE actor_id = :actor_id
LIMIT 1');
$stmt->execute( array( ':actor_id' => $actor_id ) );
return $stmt->fetch( PDO::FETCH_OBJ );
}
in example above Why MYSQL select WHERE... :actor_id
followed by this :
column and same thing in return $stmt->fetch( PDO::FETCH_OBJ );
Why use double column ?
Upvotes: 2
Views: 410
Reputation: 161
first question in example above Why MYSQL select WHERE... :actor_id followed by this : colon
$stmt->execute( array( ':actor_id' => $actor_id ) );
this statement are using some like a variable calling placeholder, by using : colon you can parse your variable into sql.
second part..
return $stmt->fetch( PDO::FETCH_OBJ )
Why use double column ?
this is as your definition target the specific object in PDO.
Upvotes: 1
Reputation: 4868
The double colon at the end is completely different. It's a separate kind of notation that is totally unrelated to the single colon in the query.
A double colon means that you want to access a constant or function that is defined in a class. If it were defined in an object you'd use the more familiar ->
notation; in this case FETCH_OBJ
is a constant inside of the PDO class itself, not in any specific PDO object. When you pass that constant as an argument to fetch()
, it tells the function to return a row in the form of an object rather than an array.
The double colon is called the scope resolution operator. It also goes by the name "paamayim nekudotayim" which is Hebrew for "double colon" and probably one of the worst naming convention choices in the history of computer science.
Upvotes: 1
Reputation: 160973
:colunm_name
is used as a placeholder, just like ?
.
The difference is with :column_name
you could use associative array to bind the parameters.
Upvotes: 2
Reputation: 4044
The string :actor_id is not a variable. It's a placeholder to bind the actual variable $actor_id value to the SQL string.
Your $stmt string is a query with a placeholder.
The actual value of the where clause is passed using the execute() method upon execution.
Why would you want to do this? PDO allows for prepared statements, and its binding helps to abstract away having to escape the variable's string as you would if you were just concatenating the $actor_id into your SQL string.
This helps with query re-usability and security.
Upvotes: 3