Problems with SELECT option with PDO

2

Friends re-edited the PDO connection below:

<?php
$conn = new PDO( 'mysql:host=localhost;dbname=pbfjacar_site' , 'pbfjacar_murilo' , 'smc100164' );
$stmt = $conn-> prepare('SELECT font FROM fontes');
$stmt-> execute();
$result = $stmt-> fetchAll(PDO::FETCH_ASSOC);
?>

To perform this new operation:

<select name="tipo_font_end">
<?php foreach( $result as $row ) ?>
<option value="<?php echo $row['font'];?>"><?php echo $row['font'];?></option>

</select>

But I'm not getting the expected result as it now pulls only the last "ID" from a total of 10 entries in the "fonts" table, as shown below:

I'menteringthetestaccessaddressbelowsoyoucanseewhat'sgoingon! link

If friends can show me where I'm wrong or even what is missing, I'll be very grateful. Hugs to all.

    
asked by anonymous 10.05.2015 / 03:17

1 answer

2
$conn = new PDO( 'mysql:host=localhost;dbname=DATABASE' , 'USUARIO' , 'SENHA' );
$stmt = $conn-> prepare( 'SELECT * FROM fontes' );
$stmt-> execute();
$result = $stmt-> fetchAll( PDO::FETCH_ASSOC );

<select name="tipo_font_end">
    <?php foreach( $result as $row ) ?>
        <option value="<?php echo $row['font'];?>"><?php echo $row['font'];?></option>
    <?php } ?>
</select>

You were using fetch , which is used to return a single line DOC

  

Gets the next row of a result set

The correct would be fetchAll to return an array of multiple items DOC a>

  

Returns an array containing all rows from the result set

OBS : If you are only going to use the name of the source, then change SELECT * FROM fontes to SELECT font FROM fontes , and if you want to use of the source and the ID then use SELECT id, font FROM fontes ... Avoid using SELECT *

    
10.05.2015 / 04:32