简体   繁体   中英

Need to fetch data from MySQL query

I have table name reffer which containing record given below MySQL query.

CREATE TABLE `reffer` (
      `reffer_id` int(11) NOT NULL,
      `seller_id` int(11) NOT NULL,
      `register_id` int(11) NOT NULL,
      `level` int(11) NOT NULL,
      `reffer_by` int(11) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    --
    -- Dumping data for table `reffer`
    --

    INSERT INTO `reffer` (`reffer_id`, `seller_id`, `register_id`, `level`, `reffer_by`) VALUES
    (1, 462, 580, 1, 462),
    (2, 462, 581, 2, 580),
    (3, 462, 582, 2, 580),
    (4, 580, 583, 2, 580),
    (5, 462, 584, 3, 583),
    (6, 462, 585, 4, 584),
    (7, 462, 586, 5, 585),
    (8, 462, 587, 6, 586);

I need a mysql query or php function to get all the users which is reffered by register_id = 580.

  1. In this image 580 is reffered by 462 and 581,582,583 all these reffered by 580 and 584 is reffered by 583 , 585 is reffered by 586 and so on.
  2. 582 has no child found.
  3. 583 has 584,585,586,587
  4. 580 has 581 to 5587

I need this type of query that find record like 2,3,4 point.

I have tried mysql query but not getting same result.

select * from reffer where reffer_by = 582

But not getting correct result.

I have also tried it for nested function in php like :

function reffered($user_id=null,$refferedData=null){
   if(count($refferedData) == 0 || empty($refferedData)){
    $refferedData = array();
   }

   $sql_refer = mysql_query("select register_id from reffer where reffer_by = ".$user_id);
   $rows_reffer = mysql_fatch_assoc($sql_refer);
   $count_reffer = mysql_num_rows($sql_refer);
   if($count_reffer != 0){
    $refferedData[] = $rows_reffer['register_id'];

    reffered($rows_reffer['register_id'],$refferedData);
   }else{
       return $refferedData;
   }

}

There's a lot of issues in your code, I'm going to go ahead and rewrite your function and use comments to explain my thoughts and the fixes.

function reffered($user_id=null,$refferedData=null){
     // as $refferedData defaults to a non-array (null) we 
     // should check if it is an array, then if it's empty 
     if(!is_array($refferedData) || empty($refferedData)){
          $refferedData = array();
     }

     // your original code was NOT sql injection safe (see https://en.wikipedia.org/wiki/SQL_injection)
     // this code forces the user input to be a safe integer
     $query = sprintf('select register_id from reffer where reffer_by = %d', $user_id);

     // this assumes we've already got a valid mysql_connect() running somewhere
     $sql_refer = mysql_query($query);

     // we should check if we have results FIRST before running the fetch_assoc
     if(mysql_num_rows($sql_refer)) {
          // we loop through the results and add register_id to the array stack,
          // while passing in the register_id to the next recursion
          while($row = mysql_fetch_assoc($sql_refer)){
               $refferedData[] = $row['register_id'];
               $refferedData = refferedData($row['register_id'], $refferedData);
          }
     }
    return $refferedData;
}

This code is untested, as recursion such as this can be very dangerous. If you have any questions or a better example of what you're trying to accomplish, I would love to offer more help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM