Error to the popular SELECT - Warning: mysql_fetch_array() expects Parameter 1 to be Resource

Asked

Viewed 185 times

0

I’m not getting popular my select.

Follow my code below:

 <select>
            <option>Selecione</option>

            <?php while($serv = mysql_fetch_array(getAllServicos())) { ?>
            <option value="<?php echo $serv['Codigo'] ?>"><?php echo $serv['Descricao'] ?></option>
            <?php } ?>

 </select>

Function getAllServices():

function getAllServicos(){
$database = open_database(); 

$sql = "SELECT Codigo, Descricao FROM `tblservico` WHERE `Status` = true";
$result = $database->query($sql);

if ($result->num_rows > 0) {
    $found = $result->fetch_all(MYSQLI_ASSOC);
}
close_database($database);
return ($found);    
}

Error:

Warning: mysql_fetch_array() expects Parameter 1 to be Resource, array Given in

1 answer

0


getAllServicos() returns an array with the database results, it makes no sense to pass it to the legacy function mysql_fetch_array(). You can go right through a foreach:

<?php foreach(getAllServicos() as $serv) { ?>
    <option value="<?php echo $serv['Codigo'] ?>"><?php echo $serv['Descricao'] ?></option>
<?php } ?>

In the printf() style ;)

<?php
    foreach(getAllServicos() as $serv) {
        printf('<option value="%s"> %s </option>', $serv['Codigo'], $serv['Descricao']);
    }
?>
  • It worked, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.