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:
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.
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?
– Murilo Cabral
SELECT * FROM fontes
will take everything from sources, do the test, access your consoleBD
and insert:SELECT * FROM fontes
and see the result.– Ricardo
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)
– Murilo Cabral
I edited a line in my code, run it and tell me what you see.
– Ricardo
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...".
– Murilo Cabral
Strange was to ta returned everything, and this foreach, see if it is running, put an echo 'q<br/>';
– Ricardo
Strange also is that in a select your is used
From fontes
but on the table tafont
.– Ricardo
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.– Edilson
No use loop as there is only one item. The error is the use of
fetch
when it should befetchAll
.– Papa Charlie
@Papacharlie, correct I saw now the author is using the same code that aimed to recover the amount of results of a query.
– Ricardo
@Murilocabral, check now I switched the
fetch
forfetchAll
.– Ricardo