Skip to content
Advertisement

How to use prepared statements with Postgres in PHP

The code below is my prep to eventually update a database with the prices of shares. What I am trying to do is get the value of the fund_name, based on its row number. The row is found, based on which row a value is entered on. The echo statements are just for me now, to see what is being returned before I modify it to update the DB.

The code below works somewhat. It will return the value amount I entered ($price[$i]) and the count ($x) it is on. The $x does correspond correctly to what row I entered the value on. However, when I try to execute the SELECT statement, nothing is returned if I have a variable in the select statement. If I change the $x in the SELECT statement to a number, ie 3, it will return the correct fund_name that is on line 3. I have tried by putting fund_line = '$x' and fund_line = $x directly in the SELECT statement. I have also tried fund_line = ? when using the $x as $update_prices->execute([$x]). I don’t get an error message when using a variable, just nothing is returned for the fund_name, while the other echo statements $price[$i] and $x return what I expect them to.

Is there something I am missing?

JavaScript

Advertisement

Answer

You query is OK. you can pass the parameter like below with ?:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement