PHP内爆值
如何从下面的数组中获取单个值?第19行的当前结果类似于重复两次的19 E:/database/users/1919/PHP内爆值,php,implode,Php,Implode,如何从下面的数组中获取单个值?第19行的当前结果类似于重复两次的19 E:/database/users/1919/ <?php $db = new SQLite3('C:/inetpub/wwwroot/database.db'); $row = $db->query('select id from users'); while ($lastrow = $row->fetchArray()) { $id = implode($lastrow); } $database = '
<?php
$db = new SQLite3('C:/inetpub/wwwroot/database.db');
$row = $db->query('select id from users');
while ($lastrow = $row->fetchArray())
{
$id = implode($lastrow);
}
$database = 'E:/database/users/'.$id.'/';
?>
我只需要19,所以URL看起来像
E:/database/users/19/
<?php
$db = new SQLite3('C:/inetpub/wwwroot/database.db');
$row = $db->query('select id from users');
while ($lastrow = $row->fetchArray())
{
$id = implode($lastrow);
}
$database = 'E:/database/users/'.$id.'/';
?>
默认情况下使用SQLITE3\u BOTH
模式,该模式返回所选列的0索引关联数组
这意味着您将收到如下值:
$lastrow = array(
0 => '19',
'id' => '19'
);
要解决此问题,只需更改fetchArray
即可使用SQLITE3\u NUM
或SQLITE3\u ASSOC
检索单个值
$row = $db->query('select id from users');
while ($lastrow = $row->fetchArray(SQLITE3_NUM)) {
$id = implode($lastrow);
//list($id) = $lastrow; //alternative to implode
}
$database = 'E:/database/users/'.$id.'/';
或者,您只需要指定要检索的列名,而不需要使用内爆
$row = $db->query('select id from users');
while ($lastrow = $row->fetchArray()) {
$id = $lastrow['id'];
}
$database = 'E:/database/users/'.$id.'/';
由于您似乎只需要最后一行,因此我建议更改查询以提高总体性能
SELECT MAX(id) FROM users
或
允许您检索最后一个id,而无需遍历整个users表
if ($row = $db->query('SELECT MAX(id) FROM users')) {
if ($row->numColumns()) {
list($id) = $row->fetchArray(SQLITE3_NUM);
$database = 'E:/database/users/'.$id.'/';
}
}
由于
$lastrow
的值是正确的数组,并且您说过它会得到两次值,因此您可以尝试这样做,声明$id=array()
并尝试将值重新分配给$id=array\u shift($lastrow)
然后是另一个处理该值的变量,如$inpodedarray=inpode($id)
并设置为您喜欢的$database='E:/database/users/'。$inpulodedarray'/'代码>这应该得到值19
。这是一个操作数组的想法。是否要为每个id创建URL?我可以确认这对我有效。但必须以@fyrye作为答案。非常感谢。