2
I’m retrieving data from the database that would be "The Nominee’s Indicator," only it takes them all until it recovers all. What I need to do is get only the first 3 results.
And if I can indicate how many results I want to get, that is, set it in the function or in some variable and when I get the value X of the variable, then it stops searching because there is no more need.
Actually I did this function, but it’s returning all the right data, but it’s returning more than 3 and I only want 3 for now. I need to get the first three only.
<?php
$db = mysql_connect('localhost', 'root', 'vertrigo');
$bd = mysql_select_db('cotas', $db);
$array = array();
function indicadores($id){
global $array;
$query = mysql_query("SELECT * FROM indicadores WHERE id_indicado = '$id'");
if(mysql_num_rows($query) > 0){
$fetch = mysql_fetch_object($query);
$id = $fetch->id_indicador;
$array[] = $id;
indicadores($id);
}
}
indicadores(10);
echo var_dump($array);
?>
to improve use order by id_indicated desc to pick up the last record inserted in your BD
– ndroid
I forgot this detail, thanks for commenting.
– André Ramos
You don’t need recursion to solve this, I think this query is enough.
– André Ramos
It didn’t work. It returns me an array
empty
I believe it’s because it re-executes the function until it finds the last indicator.– Alisson Acioli
try if(Count($query)>0)
– ndroid