Change a Select mysql to a Select PDO

Asked

Viewed 354 times

4

I have a select of sources registered in a table, and I currently use this code to list them so that the user can choose the desired source.

    <select name="tipo_font_end">
    <?php
    $select = mysql_query("SELECT * FROM fontes");
    while($res = mysql_fetch_array($select)){
    ?>
    <option value="<?php echo $fontes = $res['font'];?>"><?php echo $fontes = $res['font'];?></option>
    <php? } ?>
    </select>

I would like to know how to change to work with this PDO connection below.

    <?php
    $conn= new PDO("mysql:host=localhost;dbname=site", "root", "");

    $count = 'SELECT * FROM conteudo, cabecalho, rodape, fontes';
    $stmt = $conn->prepare($count);

    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    return $result;
    ?>

I’m inserting image from Source Table: Tabela das Fontes

and I am attaching.php connection code to be checked if I did it correctly.

    <?php
    $conn= new PDO("mysql:host=localhost;dbname=pbfjacar_site", "root", "");

    $count = 'SELECT * FROM conteudo, cabecalho, rodape, fontes';
    $stmt = $conn->prepare($count);

    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    return $result;

    // Código para Select de fontes da rodapa.php

    $count = 'SELECT * FROM fontes';
    $stmt = $conn->prepare($count);

    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    return $result;

    foreach($result as $res){
      echo $result['font'];
    }
    ?>

Already my thanks for the attention of all the friends.

1 answer

1

<?php
$conn= new PDO("mysql:host=localhost;dbname=site", "root", "");

$count = "SELECT * FROM font";
$stmt = $conn->prepare($count);

$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

 foreach($result as $res){
      echo $res['font'];
 }
?>

Within the foreach to print properly to your need.

  • Ricardo put this PDO code inside the connection.php file, and it worked in part, because he is only looking for the first table id, where it has plus 9 names of registered sources. How do I fetch everyone?

  • SELECT * FROM fontes will take everything from sources, do the test, access your console BD and insert: SELECT * FROM fontes and see the result.

  • Ricardo I checked and there are 10 entries in the source table as image that I will insert editing my question. I am attaching the address so that you. can analyze what is happening. (http://www.pbfjacarepagua.com.br/teste/end.php)

  • I edited a line in my code, run it and tell me what you see.

  • From what I’ve noticed, you. withdrew "Return $result;". I changed and uploaded the file to the server, and checked the address (http://www.pbfjacarepagua.com.br/teste/end.php). But it continues to bring only the first ID with the name "Select Source...".

  • Strange was to ta returned everything, and this foreach, see if it is running, put an echo 'q<br/>';

  • Strange also is that in a select your is used From fontes but on the table ta font.

  • Hello, put the return in the loop. When you declare $result = x, assigned to the variable $result only the first set of values returned by the query.

  • No use loop as there is only one item. The error is the use of fetch when it should be fetchAll.

  • 1

    @Papacharlie, correct I saw now the author is using the same code that aimed to recover the amount of results of a query.

  • @Murilocabral, check now I switched the fetch for fetchAll.

Show 6 more comments

Browser other questions tagged

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