skip to Main Content

It’s a very weird kind of problem.

Here’s my code :

function lvlmem($a_id)
{   
    global $con;
    global $mem;
//                  $a_id = "'".$a_id."','220'";
    $results = [];
    $stmt = $con->prepare("SELECT id FROM `$mem` where parent_id in (?) && (plan!='bm' || takencf=1) && martyr!=1 && status='verified'");
    $stmt->bind_param("s", $a_id);
    $stmt->execute();
    $result3 = $stmt->get_result();
    $stmt->close();

    echo "<br>SELECT id FROM `".$mem."` where parent_id in (".$a_id.") && (plan!='bm' || takencf=1) && martyr!=1 && status='verified'";
    while ($row3 = $result3->fetch_assoc())
    {
        $results[] = $row3['id'];
        var_dump($row3['id']);
    }
    return $results;
}
$lvl1 = lvlmem($row2['id']);  // row2[id] is a no like 219
$imp = implode("','", array_map('intval', $lvl1));
$lvl2 = lvlmem($imp);
$imp = implode("','", array_map('intval', $lvl2));
$lvl3 = lvlmem($imp);
$imp = implode("','", array_map('intval', $lvl3));
$lvl4 = lvlmem($imp);
$imp = implode("','", array_map('intval', $lvl4));
$lvl5 = lvlmem($imp);
$imp = implode("','", array_map('intval', $lvl5));
$lvl6 = lvlmem($imp);
$imp = implode("','", array_map('intval', $lvl6));
$lvl7 = lvlmem($imp);
$imp = implode("','", array_map('intval', $lvl7));
$lvl8 = lvlmem($imp);
$imp = implode("','", array_map('intval', $lvl8));
$lvl9 = lvlmem($imp);
$imp = implode("','", array_map('intval', $lvl9));
$lvl10 = lvlmem($imp);
$imp = implode("','", array_map('intval', $lvl10));

$count = count($lvl1)+count($lvl2)+count($lvl3)+count($lvl4)+count($lvl5)+count($lvl6)+count($lvl7)+count($lvl8)+count($lvl9)+count($lvl10);

my mission: i want to get the levels

the function lvlmem is supposed to give me a array of id where parent_id matches to ids given in $a_id

so first time $a_id = 200;
and let say function returns me 201, 202, 203

then second time $a_id is going to be 201,202,203 <— the problem comes with these commas
and function should return me further ids like 206,214,219 or something

when i send the $a_id first time it goes in code like

SELECT id FROM `member` where parent_id in (219) && (plan!='bm' || takencf=1) && martyr!=1 && status='verified'

this one is successfull

but next one is not…

i tried sending it with different comma styles, every single one of them is working for me in phpmyadmin but they are not working in code


SELECT id FROM `member` where parent_id in ('219','220') && (plan!='bm' || takencf=1) && martyr!=1 && status='verified' //this doesnt bring any results. pasting same command in phpmyadmin does bring result and i am pretty sure it is happening becuas of the style of upper commas

SELECT id FROM `member` where parent_id in (220,221) && (plan!='bm' || takencf=1) && martyr!=1 && status='verified' // and quering it this way brings same result as the next one.. seems like it ignore every id after first comma btw using this query in phpmyadmin does bring the data as 221 was included in query

SELECT id FROM `member` where parent_id in (220) && (plan!='bm' || takencf=1) && martyr!=1 && status='verified'

how can i solve this issue?

btw the only important thing to me is the $count so f it can be done in any other way like by using a single query then please tell me that

this is my first time in stack overflow… so sorry if i explained it terribly… please ask in comment if you want any other information

2

Answers


  1. you can try FIND_IN-SET()

    SELECT id FROM `member` where   
     FIND_IN_SET(parent_id,("219,220"))>0    
     AND (plan!='bm' || takencf=1) AND
     martyr!=1 AND status='verified' "
    
    Login or Signup to reply.
  2. Make a

    SELECT GROUP_CONCAT(id)  as id FROM `$mem` 
    WHERE parent_id in (?) && (plan!='bm' || takencf=1) && martyr!=1 && status='verified'
    

    this gives you back a single field with comma separated ids, which you can use directly.

    I personally would write a recursive procedure for your query, but that’s me

    Your code then don’t need to be so coomplecated

    while ($row3 = $result3->fetch_assoc())
    {
        $results = $row3['id'];
        var_dump($row3['id']);
    }
    

    But as you get only one row you don’t need the while loop

    and

    you can directly use the result and count the number of values

    $result1 = substr_count( $lvl1, ",") +1; 
    $lvl2 = lvlmem($lvl1);
    $result2 = substr_count( $lvl2 , ",") +1; 
    $lvl3 = lvlmem($lvl2);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search