Jump to content

some PDO questions


AfroNinja

Recommended Posts

Much appreciation for any assistance on the following pdo topics!

 

1. The fetch() methods- can I just ignore these? The first example in a tutorial I read did a query like this:

 

$result = $db->query($sql);

foreach($result as $row)

{

//stuff

}

 

and from what I can tell, $result functions as both an associative array and a numerical array. Why then, would I need to use fetch and any of its associated parameters? Unless I explicitly need to fetch into an object or class, or I really need to display the column names (I should already know what they are if I'm fetching them) then it seems like I can skip the extra fetching...

 

2. Error handling. I switched from SILENT to WARNING. I use a try catch block on my actual database connection function so that my password doesn't display in case of an error, but I don't intend to use try catch blocks anywhere else unless I'm explicitly expecting mysql to return an error. (such as trying to insert duplicate data into a UNIQUE index column). Previously I made a custom query function that I passed $sql to and the function either did it or brought back an error. with PDO and prepared statements, writing an sql command isn't as straightforward. I noticed that when switching the errors to WARNING I got the same result as I would have previously anyway, so I was planning on leaving it like that. Any issues here?

 

3. bindValue vs bindParam. Most places I see use bindParam, so that the variables binded can later be changed and the query can be re-executed with different values. Should I use bindValue when I know this isn't the case and I'll only need to do the query once?

 

4. Skipping out on bindValue/Param. Related to the above- I saw someone mention that changing values in this fashion with bindParam wasn't good, and instead you should just do $stmt->execute($array_of_params) each time, changing the array as needed. This is fine, but when you bind a variable, that's when you state what type of data it is, IE PARAM_INT or PARAM_STR. If you skip this step, does PDO implicitly check the datatypes in order to properly sanitize the values?

Link to comment
https://forums.phpfreaks.com/topic/126826-some-pdo-questions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.