oop - PHP PDO Database class parameter passing -


I want to set up an array with multiple parameters in it, pass it into my database class Currently I only assign one parameter I can send, but generally my SQL queries have many parameters.

Sample database method:

  public function fetchQuery ($ Sql, $ params, $ conn) {try {$ queryparams = array ($ Params); $ X = $ conn- & gt; Ready ($ SQL); $ X- & gt; Execution ($ queryparams); $ RArray = $ x- & gt; Get All (PDO :: FETCH_ASSOC); Return $ Rier; } Hold (exception $ e) {die (print_r ($ e-> getMessage ())); }}   

Call the database method:

  $ params = $ vendID; $ Sql ​​= "Contact email where Vendid =?" Choose; {$ Email = $ db- & gt; Try Fetchquio ($ sql, $ params, $ conn); $ eCount = Count ($ email); If ($ eCount> gt;) {foreach ($ line $ email) {$ eArray [] = $ line ['email']; }} Else {resonance "email addresses not associated with this vendor"; }} Hold (exception $ e) {die (print_a ($ e-> getMessage ()); }   

Now say that my SQL query was: "Choose from contacts * where CustID =? and expired?" How would I build an array and pass my function?

Every time I try, I get an array for conversion error string.

Thanks

Now to say that my SQL query was: Select "From Contacts * where CustID =? And Expired?" How will I build an array and pass my work?

I $ queryparams = array ($ params); I will remove the part of my code, and always pass the function to an array, even if it has only one element.

  $ params = array ($ custID); $ Sql ​​= "Contact email where Vendid =?" Choose; {$ Email = $ db- & gt; Try Fetch Queries ($ sql, $ params, $ conn);   

mutiple

  $ params = array ($ vendID, $ has expired); $ Sql ​​= "Choose from * Contact * where CustID =? And expired?"; {$ Email = $ db- & gt; Try Fetchquio ($ sql, $ params, $ conn);   

I personally prefer the named parameter, where I can use the associative array:

  $ sql = "select * From contacts where CustID =: custId and expired =: "expired"; $ params = array (': custId' = & gt; $ custID, ': expired' = & gt; $ has expired);    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -