Jump to content

Creating a prepared statement from an array


KillGorack

Recommended Posts

[[ split from Creating a prepared statement from an array --requinix ]]

 

I used the PDO example on the first code mac_gyver provided. That works great, and it seems I can just about understand it.

 

One more question if it's OK to revive this thread..

 

I tried to stick "PDO::PARAM_STR" into a variable and call it out in a variable like below.

 

Strings "STR"

boolean "BOOL"

Integers "INT"

and so on..

 

If I place just PDO::PARAM_STR with no quotes it all works fine for it seems all data types.

If I cram that parameter into a variable (within an array) and call it like below it errors.

 

 

 

        foreach($fldarray as $fld){
          if($fld[9] == 1){
            $insertarray[] = array('name'=>$fld[2], 'type'=>$fld[30], 'value'=>$fld[25]);
          }
        }
Simple question is it OK to use PDO::PARAM_STR for everything?
Link to comment
Share on other sites

Do not resurrect ancient threads. If you think it's super-important that we read the entire previous discussion before answering your question (which it isn't), then link to to thread.

 

But don't create a new thread now. I'll see if a moderator can split your thread.

 

 

 

Simple question is it OK to use PDO::PARAM_STR for everything?

 

Simple answer: It depends.

 

MySQL is pretty much the only database which doesn't care about types, so if you want your code to be portable and not just work for MySQL, then it's not OK to use strings for everything.

 

But even if you want to restrict yourself to MySQL forever, you can run into crazy effects when relying on automatic type conversion. So if you want your code to be robust, it's not OK either.

Link to comment
Share on other sites

 

PDO::PARAM_STR is a constant defined with an integer value.

 

There is absolutely no reason that you should have an issue assigning the value to an array element and using it later. It's an integer value when it's passed.

 

If I cram that parameter into a variable (within an array) and call it like below it errors.

We would have to see the code that does this "cramming" to understand whether your technique is valid or doing something different than what you think it is.

 

With that said:

 

$foo['type'] = PDO::PARAM_STR;
Is entirely different from:

 

$foo['type'] == 'PDO::PARAM' . '_STR';
We need to know the specifics of what you were trying to do there that lead you down this path in the first place.
Link to comment
Share on other sites

Hmmm...  

 

If one skips the bind process and instead uses the syntax:

 

$parms = array(
       ':fld1'=>$value1,
      ':fld2'=>$value2);
$qresult = $q_st->execute($parms);

 

one does not need worry at all about setting variable types.

 

One less thing to sweat.

Link to comment
Share on other sites

Except for:

 

MySQL is pretty much the only database which doesn't care about types, so if you want your code to be portable and not just work for MySQL, then it's not OK to use strings for everything.

 

But even if you want to restrict yourself to MySQL forever, you can run into crazy effects when relying on automatic type conversion. So if you want your code to be robust, it's not OK either.

Link to comment
Share on other sites

I was trying to put quotes around, its working fine now. 

 

Thank ya much!

 

Sorry for the breach of protocol with the older thread, it wont happen again.

 

As for the specific reason, Just trying to create forms, for adding / modifying records using data from an array created partly by reading the meta data from the table itself. I have a few tables to take care of and only want one set of code that can handle em all.

Link to comment
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.