Using PDO links in PHP

Asked

Viewed 98 times

2

I am trying to create a loop with PDO where I can choose the desired field to position it on the page. But the code I’m using is bringing all the fields at once.

My code is like this:

include("libraries/conn.php");
$sql = "SELECT 
   content.id_content, 
   content.img, 
   content.titulo, 
   povos.pv, 
   cat.categoria, 
   content.inicio, 
   content.fim, 
   content.content, 
   regiao.reg, 
   regiao.wmap
FROM cat 
INNER JOIN regiao 
INNER JOIN povos 
INNER JOIN content ON povos.id_povos = content.civ 
AND regiao.id_regiao = povos.regiao 
AND cat.id_cat = content.clas
ORDER BY rand()";

$result = $PDO->query( $sql );
$rows = $result->fetchAll(PDO::FETCH_ASSOC);

print_r( $rows );

The result is coming like this:

Array ( [0] => Array ( [id_content] => 31 [img] => paleozoica.jpg [title] => Paleozoic Era [Pv] => Indefinite [category] => Architecture and Housing [start] => -550000000 [end] => -250000000 [content] => The Paleozoic era prevailed from 550 to 250 million years ago. In this period the terrestrial surface has undergone great transformations§ãµes, among them are the emergence of mountain ranges such as the Scandinavian Alps (Europe). Essa era geológica também se caracteriza mpela ocorrência de rochas sedimentares e metamórficas, formação de grandes florestas, glaciações, surgimento dos primeiros insetos e répteis. [reg] => World [wmap] => world.jpg )

  • I brought all the columns you selected. What would be the result you expected to get?

2 answers

2

To access some information within the variable $rows, do so:

$rows->titulo
$rows->categoria
$rows->inicio
$rows->fim
...

This way you can access the records inside the array and position them where you would like on any part of the page, and they are inside the loop.

  • Thanks for the reply Rhuan, but I’m still not getting the desired effect. My code looks like this: print_r( $Rows->title ); and the browser is returning the message "Notice: Trying to get Property of non-object in D: xampp htdocs n_archaeus index.php on line 14"

1


Note that the array $rows holds a key 0 with the value being another array:

Array ( [0] => 
    Array ( 
        [id_content] => 31 
        [img] => paleozoica.jpg 
        [titulo] => Era Paleozoica 
        [pv] => Indefinido 
        ...

So to filter the result, for each key, you have an array with the data, that is, we have a loop foreach() ai, for each $rows let’s separate your key and array with the values you need:

foreach ($rows as $key => $eachRow) {
    echo $eachRow['id_content'];
    echo "<br>";
    echo $eachRow['img'];
    echo "<br>";
    echo $eachRow['titulo'];
}

How you used the constant FETCH_ASSOC to bring the results, the array $rows has as content another array, so $eachRow['id_content'], $eachRow['img'], etc, but if used FETCH_OBJ, for example, the array $rows would have an object as content, and could be expressed within the loop foreach() thus $eachRow->id_content, $eachRow->img, etc..

Browser other questions tagged

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