Php 将数组字符串转换为Int始终得到返回Int(1)
我尝试使用intval将字符串数组转换为INT 但是结果/返回总是int(1) 而数据库值为14698 我的数据库:Php 将数组字符串转换为Int始终得到返回Int(1),php,mysql,Php,Mysql,我尝试使用intval将字符串数组转换为INT 但是结果/返回总是int(1) 而数据库值为14698 我的数据库: +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | id | i
+-------+---------+------+-----+---------+----------------+
| 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 |
+----+------+-------+
<?php
require '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];
<?php
require 'config.php';
function queryInt($query) {
global $conn;
$result = mysqli_query($conn, $query);
$rows = [];
while ($row = mysqli_fetch_assoc($result)) {
$rows [] = intval($row);
}
return $rows;
}
array(1) { [0]=> int(1) } 1
这是我的crudQuery.php页面:
+-------+---------+------+-----+---------+----------------+
| 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 |
+----+------+-------+
<?php
require '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];
<?php
require 'config.php';
function queryInt($query) {
global $conn;
$result = mysqli_query($conn, $query);
$rows = [];
while ($row = mysqli_fetch_assoc($result)) {
$rows [] = intval($row);
}
return $rows;
}
array(1) { [0]=> int(1) } 1
为了从数据库im而不是1返回值,我支持做什么
while ($row = mysqli_fetch_assoc($result)) {
$rows [] = intval($row);
}
mysqli\u fetch\u assoc
返回一个数组,您不能将数组强制转换为int,并期望得到有意义的结果
intval($row)
需要是intval($row['rate'])
changevar\u dump($rate);echo$rate[0]代码>至var\u转储($rate);echo美元汇率[1]代码>