Problems with SELECT option with PDO

Asked

Viewed 1,150 times

2

Friends reissued 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 execute 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, because it now pulls only the last "ID" of a total of 10 entries in the table "sources", as shown below: inserir a descrição da imagem aqui

I’m entering the test access address below, so you can see what’s going on! http://www.pbfjacarepagua.com.br/teste/end.php

If friends can show me where I am going wrong or even what is missing, I would be very grateful. Hugs to all.

  • Your <select> no loop?

  • Friend what would be loop? I am very lay in php.

  • 1

    Before that line return $result; put this var_dump($result);, if possible edit your response with the result, ok?

  • It didn’t work!!! Enter the address (http://www.pbfjacarepagua.com.br/teste/end.php), and see the result. The code looks like this: <?php&#xA; $conn= new PDO("mysql:host=localhost;dbname=site", "root", "");&#xA;$count = 'SELECT * FROM fontes';&#xA;$stmt = $conn->prepare($count);&#xA;$stmt->execute();&#xA;$result = $stmt->fetch(PDO::FETCH_ASSOC);&#xA;var_dump($result);&#xA;return $result;&#xA;foreach($result as $res)&#xA;?>

  • 8

    Murilo, I noticed that you have completely edited your publication, which has misread a question that could be useful to others, because now whoever enters here will see things totally out of context. If any answer solved your problem you can mark as solved (do not need to put SOLVED in the title) and add information in the comments. If you’ve solved it yourself, create your own answer. But it’s good to always keep your initial question.

  • I’m sorry Paul, it won’t happen again.

Show 1 more comment

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

Get the next line of a result set

The right thing would be fetchAll to return an array of multiple items DOC

Returns an array containing all the lines of the result set


OBS: If you will only make use of the name from the source, then change SELECT * FROM fontes for SELECT font FROM fontes, and if you intend to use the name of the source and the ID then use SELECT id, font FROM fontes... Avoid using SELECT *

  • Papa I will edit my question for you. see how the codes looked, and check if I missed something, thanks? And this line of SELECT '<? php } ? >', causing error "Parse error: syntax error, Unexpected '}' in /home/pbfjacar/public_html/test/end.php on line 19". When I withdraw it brings me only the last registered item. Check the test address (http://www.pbfjacarepagua.com.br/teste/end.php)

  • Solved Pope, we were forgetting to insert a key in the line of "foreach", so was giving the error that mentioned in the previous comment. Well summing up, with your help I managed to pull all the items from the source table. Thank you so much for your help, hugs.

Browser other questions tagged

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