PHP PDO question. No bind_param used

Hello again. As I mentioned in another post, I used to love coding, but I only do it as a hobby now and am interested in your opinion on this code. It is several examples, but the Bind_Param was never used. Can anyone tell me if this is a CORRECT way to handle Prepared Statements? Thank you so much. I value the community opinion very much and it has always been helpful to me.

//	FETCH MULTIPLE POSTS

	$author = 'Brad';
	$is_published = true;
	$id = 1;

//	Positional Params

	$sql = 'SELECT * FROM posts WHERE author = ?';
	$stmt = $pdo->prepare($sql);
	$stmt->execute([$author]);
	$posts = $stmt->fetchAll();

	foreach ($posts as $post){
		echo $post->title . '<br>';
	}	

//	Named Params

	$sql = 'SELECT * FROM posts WHERE author = :author && is_published = :is_published';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(['author' => $author, 'is_published' => $is_published]);
	$posts = $stmt->fetchAll();

	var_dump($posts);

	foreach ($posts as $post){
		echo $post->title . '<br>';
	}

//	FETCH SINGLE POST

	$sql = 'SELECT * FROM posts WHERE id = :id';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(['id' => $id]);
	$post = $stmt->fetch();

	echo $post->body;

//	GET ROW COUNT

	$stmt = $pdo->prepare('SELECT * FROM posts WHERE author = ?');
	$stmt->execute([$author]);
	$postCount = $stmt->rowCount();
	
	echo $postCount;

//	INSERT DATA

	$title = 'Post Five';
	$body = 'This is Post Five.';
	$author = 'Kevin';
	
	$sql = 'INSERT INTO posts(title, body, author) VALUES (:title, :body, :author)';
	$stmt = $pdo->prepare($sql);
	$stmt->execute(['title' => $title, 'body' => $body, 'author' => $author]);
	echo 'Post Added!';

Are those working for you?

I have always used a leading colon with the named placeholders so I was unaware that they might not be needed.

Yes, Mittineague. All the examples worked fine on my localhost server using PHP 5.6 and Mysql 5.7.

What’s a leading colon?

Instead of
$stmt->execute(['author' => $author, 'is_published' => $is_published]);

like this
$stmt->execute([':author' => $author, ':is_published' => $is_published]);

though it might have something do with my habit of doing it like this
$stmt->execute(array(':author' => $author, ':is_published' => $is_published));

Is the code I posted SIMILIAR to how you code, Mittineague? Do you use bind_param in your coding?

The only time I don’t use bound parameters is when there is absolutely no user supplied input in the query. The majority of time I write multiple lines of ->bindParam followed by ->execute() mostly because of habit and I find them easier to read.

1 Like

The difference between yours and @Mittineague’s example is that he’s got the : before any key index. This is how you’re supposed to bind the parameters in PDO. If you just do

$stmt->execute(['author' => $author, 'is_published' => $is_published]);

As Mittineague said, then you’ll actually be inserting or updating them yourself. So for example. If the person types in say John Smith for the author name. What will actually be inserted instead is author. Or if you insert a date for is_published, then what actually gets inserted or updated is is_published and not the actual date. So you need to use : because the distinguishes your literal interpretation from placeholders.

Is there a reason to use bindParam rather than just putting the array into the execute or is it just a preference/habit of yours?

I normally prefer the square brackets in my execute, I guess it just seems neater, but have been caught out and had to revert on a site where the client is still running PHP5.2 :rolleyes:

In that case your client really must update to a newer PHP version. 5.2 has been end of life for a while now.

1 Like

@gregs - For what it is worth, I never use binds. They are basically a holdover from the mysql C interface. So your first example is good.

@Mittineague - It is buried somewhere in the docs but while you need the colon in the sql statement, it is optional when it comes to array keys. 'author and ‘:author’ will both work. Just one of the PDO things to help keep you on your toes.

Having said that, named parameters are overrated and PDO specific. I always just use ? placeholders. You might think that named parameters provide better long term documentation and are easier to maintain but I found them to be more work than they are worth. Naming things is hard.

1 Like

It’s not just me then, I never use them either.
That’s why I asked:-

In case I was missing something, but I guess not.

I know, I know.
When I have access to do it myself, I will switch them to a newer version. But there is one I know still on it. I should maybe have a word.

My own personal preferenece is to use bound parameters, when there is any external value being plugged into a query, no matter what the source, that way I don’t accidentally create a security hole of the source later gets changed to that supplied by a user

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.