3

I have a while loop from which I'm Fetching Name from database, How can I add all the name fetched from database into that array

Here is How I fetch Name From Database

<?php $a = mysqli_query($connecDB, "SELECT * FROM video WHERE post_id='$puser'"); 
while( $b = mysqli_fetch_array($a)){?>
              name: <?php echo $b['name'];?><br>
              <?php } ?>

This Results in

name: example1
name: example2 & so on......

i want to put all these name into array like this->

array("example1", "example2", "example3");
0

4 Answers 4

2

You need to create a new array variable and assign value to that array varaible like below:-

<?php 
$new_array = array(); // create an empty array variable
$a = mysqli_query($connecDB, "SELECT * FROM video WHERE post_id='$puser'"); 
while( $b = mysqli_fetch_array($a)){
             $new_array[] = $b['name']; // assign value to array
}
echo "<pre/>";print_r($new_array); // print array
?>

output should be look like what you want:- array("example1", "example2", "example3");

Sign up to request clarification or add additional context in comments.

Comments

2

You can push the results into an array. Try this:

$names = array();
while( $b = mysqli_fetch_array($a)) {
    array_push($names, b['name']);
}

This will push to the $names array and $names will look like this:

array("name1", "name2", "name3");

Comments

1

you can use this code

while( $b = mysqli_fetch_array($a)) {
    $name[]=b['name'];
}

Comments

1

Use this:

<?php 
    $names = array(); // create an empty array variable
    $a = mysqli_query($connecDB, "SELECT * FROM video WHERE post_id='$puser'"); 
    while( $b = mysqli_fetch_array($a)){
                 **array_push($names, b['name']);** //USE PUSH TO ADD VALUES AND THIS WILL AUTOMATICALLLY SEPARATE WITH A ","
    }
    echo "<pre/>";print_r($new_array);
    ?>

1 Comment

if you could describe your answer a little bit with some words, it would be more useful

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.