我嘗試使用 intval 將我的字符串數組轉換為 INT 但是結果/返回總是int(1) 而數據庫值為 14698我的數據庫:+-------+---------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+---------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || curr | char(3) | NO | | NULL | || rate | int(11) | NO | | 0 | |+-------+---------+------+-----+---------+----------------++----+------+-------+| id | curr | rate |+----+------+-------+| 1 | USD | 1 || 2 | IDR | 14698 || 3 | JPY | 112 || 4 | EUR | 1 || 5 | THB | 33 |+----+------+-------+這是我的 crudQuery.php 頁面: <?phprequire 'config.php';if (isset($_POST['save'])) { require_once 'functions.php'; $qty = $_POST['qty']; $qty = (int) $qty; $curr = $_POST['curr']; $price = $_POST['price']; $price = (int) $price; $rate = queryInt("SELECT rate FROM rate_master WHERE curr = '$curr'"); var_dump($rate);echo $rate[0];這是我的functions.php頁面:<?phprequire 'config.php'; function queryInt($query) { global $conn; $result = mysqli_query($conn, $query); $rows = []; while ($row = mysqli_fetch_assoc($result)) { $rows [] = intval($row); } return $rows; }然后, var_dump 的結果是:array(1) { [0]=> int(1) } 1我應該做什么,以便從數據庫中返回值而不是 1 ?
1 回答

月關寶盒
TA貢獻1772條經驗 獲得超5個贊
while ($row = mysqli_fetch_assoc($result)) {
$rows [] = intval($row);
}
mysqli_fetch_assoc返回一個數組,您不能將一個數組“轉換為 int”,并期望它有一個有意義的結果。
intval($row)需要是intval($row['rate'])
- 1 回答
- 0 關注
- 133 瀏覽
添加回答
舉報
0/150
提交
取消