【问题标题】:Php Sql get first value of ArrayPhp Sql 获取数组的第一个值
【发布时间】:2015-09-16 09:38:37
【问题描述】:

如何分别访问此 foreach 的第一个值和第二个值?

foreach($database->query('SELECT id,firstname,lastname FROM `staff` WHERE categorie_bit = 1') as $resultRandom) {

}

【问题讨论】:

  • 单独”是什么意思?
  • 我想打印出第一个假设 cat 值为 1 的名字到变量中,第二个以相同的方式打印到变量中

标签: php sql arrays foreach


【解决方案1】:

根据我认为你所说的,你可以做这样的事情:

$query = "SELECT `id`, `firstname`, `lastname` FROM `staff` WHERE `categorie_bit` = 1";
$rows = [];
while ($result = mysqli_fetch_assoc($database->query($query)))
{
    $rows[] = [
        "id"=>$result['id'],
        "firstName"=>$result['firstname'],
        "lastName"=>$result['lastname']
        ];
}
var_dump($rows);

【讨论】:

    【解决方案2】:
    <?
    $query= "SELECT id,firstname,lastname FROM `staff` WHERE categorie_bit = 1";
    if ($result=mysqli_query($con,$query))
      {
      $rowcount=mysqli_num_rows($result);
    
    
    print "<table width=600 border=1>\n";
    while ($get_info = mysqli_fetch_row($result)){
    print "<tr>\n";
    foreach ($get_info as $field)
    print "\t<td>$field</td>\n";
    print "</tr>\n";
    }
    print "</table>\n";
    mysqli_close($link);
    
    ?>
    

    【讨论】:

    • 还在提示mysql_*函数?
    • @dynamic 完成编辑
    猜你喜欢
    • 2015-03-03
    • 2012-06-24
    • 2016-02-19
    • 1970-01-01
    • 2017-03-04
    • 2012-05-03
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多