3

I have this piece of code from which i wish to get a single array that contains all value.

$sql = "SELECT * FROM interest where interest='".$interest."' and userid!='".$myuserid."'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) 
    {
        while($row = mysqli_fetch_assoc($result)) 
            {
                $userid = $row["userid"];

                if($searchtype == 'both')
                    {
                        $sql2 = "SELECT * FROM register where id='".$userid."' and  discover = 'on' and id!='".$myuserid."'";
                        $result2 = mysqli_query($conn, $sql2);
                        if (mysqli_num_rows($result2) > 0) 
                            {
                                while($row2 = mysqli_fetch_assoc($result2)) 
                                    {
                                        echo "<pre>";
                                        print_r($row2);
                                        echo "</pre>";
                                    }
                            }       
                    }
            }
    }

The o/p that i am getting is something like this

Array
(
    [id] => 1
    [email] => A1
    [username] =>B1 
    [password] => C1
    [gender] => C1
)

Array
(
    [id] => 2
    [email] => A2
    [username] => B2
    [password] => C2
    [gender] => D2
)
Array
(
    [id] => 3
    [email] => A3
    [username] => B3
    [password] => C3
    [gender] => D3
)

But i wish to get this all data in a single array like this

Array
(
    [0] => Array
        (
             [id] => 1
             [email] => A1
             [username] =>B1 
             [password] => C1
             [gender] => C1
        )

    [1] => Array
        (
            [id] => 2
            [email] => A2
            [username] => B2
            [password] => C2
            [gender] => D2
        )
     [2] => Array
        (
            [id] => 3
            [email] => A3
            [username] => B3
            [password] => C3
            [gender] => D3
        )
}

can anyone tell how i can do so

3
  • Can you update your question with sample output which you want to see? Do you want there to be multiple id values in the array, or do you want one id entry with a collection of values? Commented Jul 10, 2015 at 4:41
  • @Tim Biegeleisen updated my post Commented Jul 10, 2015 at 4:46
  • One refactoring: if you need only userid from the first query, why can't u change your first query to "SELECT userid FROM interest where interest='".$interest."' and userid!='".$myuserid."'" Commented Jul 10, 2015 at 5:01

3 Answers 3

1

Create an array variable like $a=array(); at the start of your code

Get row value in array $a[]=your row value(while loop), then print this outside loop you will get all value in single array print like

print_r($a);
Sign up to request clarification or add additional context in comments.

Comments

0

Take one array variable before while loop started like $user_data = array(); and in inner loop you have to set $user_data[] = $row2;

if (mysqli_num_rows($result) > 0) {
    $user_data = array();
    while($row = mysqli_fetch_assoc($result)) {
            $userid = $row["userid"];
            if($searchtype == 'both') {
                    $sql2 = "SELECT * FROM register where id='".$userid."' and  discover = 'on' and id!='".$myuserid."'";
                    $result2 = mysqli_query($conn, $sql2);
                    if (mysqli_num_rows($result2) > 0) {
                            while($row2 = mysqli_fetch_assoc($result2)) {
                                    $user_data[] = $row2;
                                }
                        }       
                }
        }
   print_r($user_data);   //Print here your user_data outside the loop.
}

4 Comments

although i get the answer can u plz tell why the result can't be printed within the loop
You got the result in loop but it display individual array record and you want whole result in single array thats why need to create $user_data variables
in this post i had a part of my code, here i had displayed only one condition of if($searchtype == 'both') but i have 2 more condition based on searchtype and i wished to print the result of every condition separately, but if i print the result outside i won't be able to get the result acc to condition
If you have got answer of posted question then you can ask new question in new post it is better to give best answer
0

You are almost near to your goal, you just need to define one array and save each row's data in it

// array which contains all rows data
$all_rows = array();                                //  <------ step 1
if(mysqli_num_rows($result2)) 
{
   while($row2 = mysqli_fetch_assoc($result2)) 
   {
      // every record data is stored here
      $all_rows[] = $row2;                          //  <------ step 2
   }
} 
if(!empty($all_rows))
{
      print_r($all_rows);
}else
{
     echo "do some thing else since zero records fetched";
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.