Php Sql获取Array的第一个值

时间:2015-09-16 09:38:38

标签: php sql arrays foreach

如何分别获取此foreach的第一个值和第二个值?

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

}

2 个答案:

答案 0 :(得分:0)

你可以从我的想法中做出类似的事情:

$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);

答案 1 :(得分:-1)

<?
$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);

?>