Jump to content

Creating a prepared statement from an array


KillGorack
Go to solution Solved by gizmola,

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? Edited by requinix
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.

Edited by Jacques1
Link to comment
Share on other sites

  • Solution

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.