1

I'm trying to create a single dimensional enumerated array of a single column of data.

$dbc_db_filenames       = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$query_db_filenames     = "SELECT file FROM transactions_tb WHERE file != ''";
$get_db_filenames       = mysqli_query($dbc_db_filenames, $query_db_filenames);
$array_rows             = array();
while($rows             = mysqli_fetch_array($get_db_filenames, MYSQLI_NUM)) {
    $array_rows = $rows;
    echo "<br /><br /><pre>";
    print_r($array_rows);
    echo "</pre><br />";
}
mysqli_close($dbc_db_filenames);

Currently it's returning multiple arrays like this:

Array
(
    [0] => 2_20140908130612381070.jpg
)

Array
(
    [0] => 2_20140908131122368017.jpg
)

Array
(
    [0] => 2_20140908130449345689.jpg
)

I want it to return an array like this:

Array
(
    [0] => 2_20140908130612381070.jpg
    [1] => 2_20140908131122368017.jpg
    [2] => 2_20140908130449345689.jpg
)

I'm comparing a directories contents with the filename that's stored in the database on upload. To get the directories contents into an array I'm using this code:

$list = scandir(UPLOAD_PATH);
echo '<br /><br /><pre>';
print_r($list);
echo '</pre><br />';

Which returns the kind of array I want:

Array
(
    [0] => 2_20140908130612381070.jpg
    [1] => 2_20140908131122368017.jpg
    [2] => 2_20140908130449345689.jpg
)

1 Answer 1

1

Have you tried this?

while($rows             = mysqli_fetch_array($get_db_filenames, MYSQLI_NUM)) {
    $array_rows[] = $rows[0];
}

echo "<pre>";
print_r($array_rows);
echo "</pre>";
Sign up to request clarification or add additional context in comments.

8 Comments

Yeah..that makes it worse. It creates tons of array sets: Array ( [0] => Array ( [0] => 2_20140908130612381070.jpg ) ) Array ( [0] => Array ( [0] => 2_20140908130612381070.jpg ) [1] => Array ( [0] => 2_20140908131122368017.jpg ) ) Array ( [0] => Array ( [0] => 2_20140908130612381070.jpg ) [1] => Array ( [0] => 2_20140908131122368017.jpg ) [2] => Array ( [0] => 2_20140908130449345689.jpg ) )
@Micah You just need to change it to $array_rows[] = $rows[0];. And initializing an empty array before the loop is always a good idea.
@Micah jeroen is right, in that case try $array_rows[] = $rows[0]; this will do... and please put print_r($array_rows); outside the while loop. _ __!
You BOTH ROCK! Between this: $array_rows[] = $rows[0]; ... and this: put print_r($array_rows); outside the while loop ... It did it. I've been hunting this down a couple hours now. Thanks for the super fast help :)
This is my first question on here and I don't know protocol. Should I go ahead and mark this as the answer..let one of you update with a fully done answer or post the update myself and mark it? Thank you!
|

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.