-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_assoc_array.php
75 lines (65 loc) · 1.37 KB
/
fetch_assoc_array.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
$conn=mysqli_connect("localhost","root","","mercado","3306");
$q1 = $conn->query("select * from funcionario");
$q2 = mysqli_query($conn,"select * from funcionario");//mesma coisa da linha 3
print_r($q1);
echo '<br>';
print_r($q2);
echo '<br>';
/*
mysqli_fetch_array():
Array (
[0] => Array (
[0] => 1 [codigo] => 1
[1] => joao [nome] => joao
[2] => cargo1 [cargo] => cargo1
)
[1] => Array (
[0] => 2 [codigo] => 2
[1] => maria [nome] => maria
[2] => cargo2 [cargo] => cargo2
)
)
*/
/*
mysqli_fetch_assoc():
Array (
[0] => Array (
[codigo] => 1
[nome] => joao
[cargo] => cargo1
)
[1] => Array (
[codigo] => 2
[nome] => maria
[cargo] => cargo2
)
)
*/
$r1 = array();
while($row = mysqli_fetch_assoc($q1)){
array_push($r1,$row);
}
print_r($r1);
echo '<br>';
$r2 = [];
foreach($q1 as $k){
$r2[] = $k;
}
//linhas 12 e 19 produzem o mesmo resultado
print_r($r2);
echo '<br>';
$linhas_tabela = '';
foreach($r2 as $v){
$linhas_tabela .= '<tr><td>'.$v['nome'].'</td><td>'.$v['cargo'].'</td></tr>';
}
echo '<table>
<thead>
<th>nome</th>
<th>cargo</th>
</thead>
<tbody>'
.$linhas_tabela.
'</tbody>
</table>';
?>