1
中的查詢結果我試圖將可用現金的查詢結果與股票價格進行比較,但它不起作用,我不明白爲什麼。我不明白爲什麼它不工作,因爲他們似乎$現金和$值是有效的數字。這是給我的問題。無法比較php
if($cash < $value)
這是整個文件
<?php
//configuration
require("../includes/config.php");
//query user's portfolio
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// Insert the stock into their portfolio
if(preg_match("/^\d+$/", $_POST["shares"])==false){
apologize("Please enter full share numbers");
}else{
$cash = query("SELECT cash FROM users WHERE id = ?", $_SESSION["id"]);
// lookup stock
$stock = lookup($_POST["symbol"]);
// calculate total sale value (stock's price * shares)
$value = $stock["price"] * $_POST["shares"];
if($cash < $value){
apologize("you do not have enough money");
}else{
//insert stock into database
query("INSERT INTO shares
(id, symbol, shares)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE shares = shares + VALUES(shares)",
$_SESSION["id"], strtoupper($_POST["symbol"]),$_POST["shares"]
);
// substract the share value from cash
query("UPDATE users SET cash = cash - ? WHERE id = ?", $value, $_SESSION["id"]);
redirect("/");
}
}
}
else
{
// render portfolio
render("buy_search.php", ["title" => "Buy"]);
}
?>
這是查詢功能
function query(/* $sql [, ... ] */)
{
// SQL statement
$sql = func_get_arg(0);
// parameters, if any
$parameters = array_slice(func_get_args(), 1);
// try to connect to database
static $handle;
if (!isset($handle))
{
try
{
// connect to database
$handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD);
// ensure that PDO::prepare returns false when passed invalid SQL
$handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (Exception $e)
{
// trigger (big, orange) error
trigger_error($e->getMessage(), E_USER_ERROR);
exit;
}
}
// prepare SQL statement
$statement = $handle->prepare($sql);
if ($statement === false)
{
// trigger (big, orange) error
trigger_error($handle->errorInfo()[2], E_USER_ERROR);
exit;
}
// execute SQL statement
$results = $statement->execute($parameters);
// return result set's rows, if any
if ($results !== false)
{
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
else
{
return false;
}
}
「query」函數在哪裏定義?我敢打賭,這不是返回一個整數,而是一個數組或對象。 – fuzic 2013-03-17 03:36:45
請做var_dump($ cash)和var_dump($ value);並向我們顯示輸出。 – Danack 2013-03-17 03:42:08
轉儲($現金)給出了這樣的陣列 ( [0] =>數組 ( [現金] => 0.00 ) ) 轉儲($值)給出了兩倍。 – tironci 2013-03-17 03:47:01