Get the first 3 results in a query mysql_query

Asked

Viewed 425 times

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);
?>

3 answers

6

If I understand correctly, just change your SQL query.

Try this:

$query = mysql_query("SELECT * FROM indicadores WHERE id_indicado = '$id' LIMIT 3 ORDER BY id_indicado DESC");
  • to improve use order by id_indicated desc to pick up the last record inserted in your BD

  • 1

    I forgot this detail, thanks for commenting.

  • You don’t need recursion to solve this, I think this query is enough.

  • 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.

  • try if(Count($query)>0)

1

I took the liberty of making some changes to your code. Note, you should not use mysql_, they are deprecated functions and will no longer work in future versions of php. Use mysqli_. Note: Test the query in a phpmyadmin or whatever you have, to see if it is returning data, if the query is correct.

<?php
$db = mysqli_connect('localhost', 'root', 'vertrigo');
$bd = mysqli_select_db($db, 'cotas');

function indicadores($id){

    global $db;

    $array = array();

    $query = mysqli_query($db, "SELECT * FROM indicadores WHERE id_indicado = '$id' ORDER BY id_indicador DESC LIMIT 3");
    if(mysqli_num_rows($query) > 0){

        while($fetch = mysqli_fetch_object($query)){
            $id = $fetch->id_indicador;
            $array[] = $id;
        }

    }

    return $array;
}

$array = indicadores(10);

var_dump($array);
  • Thanks for the modifications. I don’t actually use mysql, use CodeIgniter, I did it only in the rush here rsrsrs. The point of doing with your code is that it will only return me the records where id_indicado is equal to 10. I actually need to do the following: Renato indicated Jorge, then earns 50%. Jorge indicated Luiz, then Jorge earns 50% and Renato 40%. Luiz indicated Bruno, So Luiz wins 50%, Jorge wins 40%, Renato wins 30.. And so on. I just need to know the indicators of the right person and go up, like a ladder.

  • I got the initial description wrong. I’m sorry.

0

RESOLVED

I found this solution:

<?php
$db = mysql_connect('localhost', 'root', 'vertrigo');
$bd = mysql_select_db('cotas', $db);

$array = array();

function indicadores($id, $niveis){

    global $array;

    if($niveis > 0){

    $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, $niveis-1);

        }

    }
}

indicadores(10, 3);

echo var_dump($array);
?>

Browser other questions tagged

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