KillGorack Posted July 7, 2017 Share Posted July 7, 2017 (edited) [[ 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 July 8, 2017 by requinix Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 7, 2017 Share Posted July 7, 2017 (edited) 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 July 7, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Solution gizmola Posted July 8, 2017 Solution Share Posted July 8, 2017 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 8, 2017 Share Posted July 8, 2017 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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 8, 2017 Share Posted July 8, 2017 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. Quote Link to comment Share on other sites More sharing options...
KillGorack Posted July 11, 2017 Author Share Posted July 11, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.