[英]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.
我要做的是根据其行号获取fund_name 的值。 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.
echo 语句现在只适合我,在我修改它以更新数据库之前查看返回的内容。
The code below works somewhat.下面的代码有点工作。 It will return the value amount I entered ($price[$i]) and the count ($x) it is on.
它将返回我输入的价值金额 ($price[$i]) 和它所在的计数 ($x)。 The $x does correspond correctly to what row I entered the value on.
$x 确实正确对应于我输入值的行。 However, when I try to execute the SELECT statement, nothing is returned if I have a variable in the select statement.
但是,当我尝试执行 SELECT 语句时,如果 select 语句中有变量,则不会返回任何内容。 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.如果我将 SELECT 语句中的 $x 更改为一个数字,即 3,它将返回第 3 行上的正确fund_name。我尝试将
fund_line = '$x' and fund_line = $x
直接放在 SELECT 语句中. I have also tried fund_line =?
我也试过
fund_line =?
when using the $x as $update_prices->execute([$x])
.当使用 $x 作为
$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.使用变量时我没有收到错误消息,fund_name 没有返回任何内容,而其他 echo 语句
$price[$i]
和$x
返回我期望的结果。
Is there something I am missing?有什么我想念的吗?
$x = 0;
$update_prices = $pdo->prepare ("SELECT fund_name FROM funds WHERE fund_name = (SELECT fund_name FROM
(SELECT fund_name, ROW_NUMBER () OVER (ORDER BY fund_name ASC) AS fund_line
FROM (SELECT fund_name FROM funds) cost) AS cost_sorted WHERE fund_line = $x);");
$update_prices->execute();
if(isset($_POST['price_updates'])) {
$price = $_POST['price'];
$priceSize = count($price);
for($i=0; $i < $priceSize; ++$i) {
$x += 1;
if ($price[$i] > 0){
echo "x$: ".$x.' is the line #</br>';
echo '$'.$price[$i].' is the amount</br>';
while ($row = $update_prices->fetch()) {
echo $row['fund_name'].'</br>';
}
}
}
}
You query is OK.你查询没问题。 you can pass the parameter like below with
?
您可以使用以下参数传递
?
: :
$x = 0;
$update_prices = $pdo->prepare ("SELECT fund_name FROM funds WHERE fund_name = (SELECT fund_name FROM
(SELECT fund_name, ROW_NUMBER () OVER (ORDER BY fund_name ASC) AS fund_line
FROM (SELECT fund_name FROM funds) cost) AS cost_sorted WHERE fund_line = ?);");
$update_prices->execute(array($x));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.