0
$sql = "SELECT active FROM table_name";
$result = $conn->query($sql);

DB:

|active|
1
1
0
1 

I've got table like this and need to get that rows into an array. Is it possible to get simple array like this?:

$array = (1,1,0,1);

thanks

2 Answers 2

1
$sql = "SELECT active FROM table_name";
$result = $conn->query($sql);

$arr=array();

foreach($result as $res)
{
  array_push($arr,$res->active);
}

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

4 Comments

Thanks @Shoyeb. But when I use your code it writes: Notice: Trying to get property of non-object in website.php on line [[ array_push($arr,$res->active); ]] please dont you know what is wrong?
Change $res->active to $res['active']. because it is an array, not an object.
Hi @momouu, If I am not wrong an array is object as well
Hey @ShoyebSheikh, I'm not really familiar with objects but I think an object can only contain 1 value and array can contain multiple objects. I'm not really sure. I just distinguish them by the ->, [] and the result from print_r() or var_dump().
0
$sql = "SELECT active FROM table_name";

$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
        $arr[]= $row['active'];
    }

print_r($arr);

1 Comment

Glad that I can help :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.