-1

如何根据另一个字段的值回显一个字段?我希望我的脚本能以某种方式告诉你我想要什么。

$sth = $conn->prepare("SELECT * FROM table WHERE item = '$item'");
$sth->execute();
$row = $sth->fetch(PDO::FETCH_ASSOC);

echo ($row['amount'] WHERE $row['currency'] = 'USD');

您可以在此处查看货币如何显示为数据字段

这是我根据您的想法创建的脚本。但是对我来说,它必须如此愚蠢仍然没有意义。带有 $pdf 的底部脚本可以忽略。

foreach($conn->query("SELECT amount FROM stockpricetable WHERE itemrelation = '$row[itemnumber]'") as $row2) {
    if ($row2['exchange'] == '') { $DKK = $row2['amount'];}
    if ($row2['exchange'] == 'CHF') { $CHF = $row2['amount'];}
    if ($row2['exchange'] == 'EUR') { $EUR = $row2['amount'];}
    if ($row2['exchange'] == 'GBP') { $GBP = $row2['amount'];}
    if ($row2['exchange'] == 'SEK') { $SEK = $row2['amount'];}
    if ($row2['exchange'] == 'USD') { $USD = $row2['amount'];}
}
    $pdf->Text(26, 4, 'DKK'.$DKK);
    $pdf->Text(26, 8, 'EUR'.$EUR);
    $pdf->Text(26, 12, 'GBP'.$GBP);
    $pdf->Text(46, 4, 'SEK'.$SEK);
    $pdf->Text(46, 8, 'CHF'.$CHF);
    $pdf->Text(46, 12, 'USD'.$USD);
4

3 回答 3

1

那是遍历整个结果的问题吗?

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
   if ($row["currency"] == "USD") echo $row["amount"];
} 
于 2012-11-26T17:08:46.080 回答
1

尝试:

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
 if (strstr($row["amount"], 'USD) !== FALSE) 
   echo $row["amount"];
} 
于 2012-11-26T17:13:30.787 回答
0

它应该只是以下情况:

$sth = $conn->prepare("SELECT * FROM table WHERE item = '$item'");
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

if ($row['currency'] == 'USD') {
    echo $row['amount'];
}

虽然,这是假设 if 语句上面的代码正确地获取数据库结果,但我不能说很惭愧,我仍然使用不推荐使用的mysql_*函数。

于 2012-11-26T17:09:35.870 回答