如何将单个选定行值存储在变量中?
How to store a single selected row value in variable?
第 "cars" 行中的条目是 "BMWx1"
$sql = "SELECT cars FROM table LIMIT 1";
$result = $con->query($sql);
if ($result->num_rows > 0) {
$rows[] = $result->fetch_row();
$result->free();
}
echo json_encode($rows[0]);
输出:["BMWx1"]
到目前为止一切顺利。现在如何让我的变量 $cars 等于 "BMWx1"
例如:
echo $cars;
output: BMWx1
谢谢你
如果您只是读取一行,请不要将该行推入数组。 fetch_assoc
returns 一个关联数组,其中包含从查询中检索到的行(如果未选择任何内容,则为 false
)。只需使用它并访问您想要的列。
$result = $con->query($sql);
$row = $result->fetch_assoc();
if ($row) {
$cars = $row['cars'];
}
第 "cars" 行中的条目是 "BMWx1"
$sql = "SELECT cars FROM table LIMIT 1";
$result = $con->query($sql);
if ($result->num_rows > 0) {
$rows[] = $result->fetch_row();
$result->free();
}
echo json_encode($rows[0]);
输出:["BMWx1"]
到目前为止一切顺利。现在如何让我的变量 $cars 等于 "BMWx1"
例如:
echo $cars;
output: BMWx1
谢谢你
如果您只是读取一行,请不要将该行推入数组。 fetch_assoc
returns 一个关联数组,其中包含从查询中检索到的行(如果未选择任何内容,则为 false
)。只需使用它并访问您想要的列。
$result = $con->query($sql);
$row = $result->fetch_assoc();
if ($row) {
$cars = $row['cars'];
}