选择单个 MYSQL 列中的最新条目并将其分配给 php 变量
Selecting the latest entry in a single MYSQL column and assigning it to a php variable
我有一个 4 列 mysql table,最左边是自动增量列,我想获取名为 extra
的列的变量值,以获得最高的自动增量增量值。我相信 SQL 查询将是:
Select extra FROM motoron2 ORDER BY id DESC LIMIT 1"
$conn = new mysqli('localhost', 'myuser', 'mypass', 'mydb');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT extra FROM motoron2 ORDER BY id DESC LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
$variablevalue = $result->fetch_assoc(); // fetch the data
echo "The variable is converted to a string and its value is $variablevalue.";
// set parameters and execute
当尝试 echo "The variable is converted to a string and its value is $variablevalue."
我看到: The variable is converted to a string and its value is Array
我希望在这里看到一个整数,我做错了什么?
fetch_assoc()
函数 returns 一个关联数组,因此您可以通过列名访问值:
$variablevalue['extra']
;
我有一个 4 列 mysql table,最左边是自动增量列,我想获取名为 extra
的列的变量值,以获得最高的自动增量增量值。我相信 SQL 查询将是:
Select extra FROM motoron2 ORDER BY id DESC LIMIT 1"
$conn = new mysqli('localhost', 'myuser', 'mypass', 'mydb');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT extra FROM motoron2 ORDER BY id DESC LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
$variablevalue = $result->fetch_assoc(); // fetch the data
echo "The variable is converted to a string and its value is $variablevalue.";
// set parameters and execute
当尝试 echo "The variable is converted to a string and its value is $variablevalue."
我看到: The variable is converted to a string and its value is Array
我希望在这里看到一个整数,我做错了什么?
fetch_assoc()
函数 returns 一个关联数组,因此您可以通过列名访问值:
$variablevalue['extra']
;