PHP / MySQL:根据其ID从数组(查询结果)中打印特定值
PHP / MySQL: Print specific value from array (query result) based on its ID
我使用以下 PHP 行从 MySQL table 中查询数据。
我想将查询结果存储在一个数组中,然后在同一页上根据其 ID 从该数组中打印特定值。
我是 PHP 的新手,希望有人能帮我解释一下我在这里做错了什么或遗漏了什么。我假设我错误地创建了数组。
我的PHP(相关部分,变量在本页前面定义):
$con = mysqli_connect($host_name, $user_name, $password, $database);
$result = mysqli_query($con, "SELECT id, $col FROM $table ORDER BY id");
$rows = array();
if($result)
{
while($row = mysqli_fetch_array($result))
{
print_r($row);
}
}
var_dump($rows);
我当前的数组(如上打印时):
Array ( [0] => testid1 [id] => testid1 [1] => testval1 [en] => testval1 ) Array ( [0] => testid2 [id] => testid2 [1] => testval2 [en] => testval2 ) Array ( [0] => testid3 [id] => testid3 [1] => testval3 [en] => testval3 ) array(0) { }
示例:
例如。我想从此数组中打印 ID = "testid2" 的项目的值。
在这种情况下,打印值应该是“testval2”。
非常感谢,
麦克
将您的数据存储为:
for
(
$set = array();
$row = $result->fetch_assoc();
// use value of `id` as the key in your `$set`
$set[$row['id']] = $row
);
print_r($set);
// later:
$id = 1;
echo $set[$id]['en'];
// or
echo $set[$id][$lang];
您可以只使用 fetch_all()
然后使用列搜索
$con = mysqli_connect($host, $username, $password, $database);
$result = mysqli_query($con, "SELECT id, $lang FROM $table ORDER BY id");
$set = $result->fetch_all(MYSQLI_ASSOC);
$key = array_search('testid2', array_column($set, 'id'));
echo $set[$key]['en'];
您可以使用 array_combine()
with array_column()
将查询输出(在行和列中)转换为键值格式(键是 id
,值是 en
列) :
// $set array is populated using fetch_assoc
// create a new array using $set
$modified_set = array_combine(array_column($set, 'id'),
array_column($set, 'en'));
// Now, you can access the en value for a specific id like this:
// eg: for id = testid2
echo $modified_set['testid2']; // will display testval2
我使用以下 PHP 行从 MySQL table 中查询数据。 我想将查询结果存储在一个数组中,然后在同一页上根据其 ID 从该数组中打印特定值。
我是 PHP 的新手,希望有人能帮我解释一下我在这里做错了什么或遗漏了什么。我假设我错误地创建了数组。
我的PHP(相关部分,变量在本页前面定义):
$con = mysqli_connect($host_name, $user_name, $password, $database);
$result = mysqli_query($con, "SELECT id, $col FROM $table ORDER BY id");
$rows = array();
if($result)
{
while($row = mysqli_fetch_array($result))
{
print_r($row);
}
}
var_dump($rows);
我当前的数组(如上打印时):
Array ( [0] => testid1 [id] => testid1 [1] => testval1 [en] => testval1 ) Array ( [0] => testid2 [id] => testid2 [1] => testval2 [en] => testval2 ) Array ( [0] => testid3 [id] => testid3 [1] => testval3 [en] => testval3 ) array(0) { }
示例:
例如。我想从此数组中打印 ID = "testid2" 的项目的值。
在这种情况下,打印值应该是“testval2”。
非常感谢, 麦克
将您的数据存储为:
for
(
$set = array();
$row = $result->fetch_assoc();
// use value of `id` as the key in your `$set`
$set[$row['id']] = $row
);
print_r($set);
// later:
$id = 1;
echo $set[$id]['en'];
// or
echo $set[$id][$lang];
您可以只使用 fetch_all()
然后使用列搜索
$con = mysqli_connect($host, $username, $password, $database);
$result = mysqli_query($con, "SELECT id, $lang FROM $table ORDER BY id");
$set = $result->fetch_all(MYSQLI_ASSOC);
$key = array_search('testid2', array_column($set, 'id'));
echo $set[$key]['en'];
您可以使用 array_combine()
with array_column()
将查询输出(在行和列中)转换为键值格式(键是 id
,值是 en
列) :
// $set array is populated using fetch_assoc
// create a new array using $set
$modified_set = array_combine(array_column($set, 'id'),
array_column($set, 'en'));
// Now, you can access the en value for a specific id like this:
// eg: for id = testid2
echo $modified_set['testid2']; // will display testval2