Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAshish Mehra
Courses Plus Student 340 PointsbindParam
Hey please any one explain what we put in first parameter of bindParam function..
$result->bindParam(1,$limit,PDO::PARAM_INT); //here what 1 is
1 Answer
Simon Woodard
6,545 PointsWhen using prepared statements to query the database you can use multiple 'wildcards' or '?' in your query. So you need some way to reference these when binding your parameters.
The 1 in your bindParam statement simply refers to the first 'wildcard' or '?' in your prepared query, in turn to reference the second 'wildcard' if you had one would be to use '2'.
Case in point
$result = $db->prepare('INSERT INTO table_name VALUES (NULL, ?, ?, ?)');
$result->bindParam(1,$variable_1);
$result->bindParam(2,$variable_2);
$result->bindParam(3,$variable_3);
$result->execute();
Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 Pointsthe "1" will be the place holder in your query.
So for example if you have the query
"SELECT * FROM example WHERE limit = ?"
then when you use bindParam it will bind the 1st place holder in your query the "?" with the "$limit" variable