GePraxa GePraxa - 2 months ago 21
MySQL Question

Get query data without foreach

Currently I make my query like this:

<?php
function Data() {
$db = DB::getInstance();
$query = $db->query("SELECT * FROM users");
$results = $query->results();
return ($results);
}
$listdata = Data();
?>


And when I want to see my information I have something like this:

<div class="container">
<h1>Title</h1>
<hr>
<div class="row">
....
...
..
.
<?php foreach ($listdata as $v1) { ?>
<p><?=$v1->username?></p>
<?php } ?>

</div>
</div>


But if I want to put information see for example in the footer of the page I have to open a new
foreach


As I can transform this code to open the query to the top of the page and simply llabar variables where I want without opening another
foreach
?

Even better would defeat the function, I just want to put the query and data wherever.

Thank you!

Answer

You could try echo $listdata[0]->column_name thou this will print only the first row......so to be precise use a where clause in your sql statment

Edit: It will print only the first index in the array that contains the Fetched Data