How can I make a FOR
in php using mysqli
?
I've always been able to do this:
for($i=0; $i < $qtde; $i++){
$pet_id = **mysql_result**($dadosPets, $i, 'animal_id');
}
How do I do the same, however, with mysqli
?
How can I make a FOR
in php using mysqli
?
I've always been able to do this:
for($i=0; $i < $qtde; $i++){
$pet_id = **mysql_result**($dadosPets, $i, 'animal_id');
}
How do I do the same, however, with mysqli
?
The documentation link itself can help link (I recommend it in English, because sometimes the Portuguese version contains errors: /)
You will have to use:
A complete example:
<?php
//Conecta
$mysqli = new mysqli("localhost", "usuario", "senha", "banco");
if (mysqli_connect_errno()) {
printf("Erro de conexão: %s\n", mysqli_connect_error());
exit;
}
$query = 'SELECT ...'; //Sua select
if ($result = $mysqli->query($query)) {
for($i = 0; $i < $qtde; $i++) {
if ($result->data_seek($i)) {
$row = $result->fetch_assoc();
//ID do pet
$pet_id = $row['pet_id'];
}
}
$result->close();
}
$mysqli->close();
Procedural style:
<?php
//Conecta
$link = mysqli_connect("localhost", "usuario", "senha", "banco");
if (!$link) {
printf("Erro de conexão: %s\n", mysqli_connect_error());
exit;
}
$query = 'SELECT ...'; //Sua select
if ($result = mysqli_query($link, $query)) {
for($i = 0; $i < $qtde; $i++) {
if (mysqli_data_seek($result, $i)) {
$row = mysqli_fetch_assoc($result);
//ID do pet
$pet_id = $row['pet_id'];
}
}
mysqli_free_result($result);
}
mysqli_close($link);