0

Below is the PHP code to retrieve data:

<?php
$sql = $myquery;
$result = $conn->query($sql);

$QueryArray = array();
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

    $QueryArray[] = Array($row["Column1"], $row["Column2"], $row["Column3"]);

    }

} else {

}
?>

And on the HTML side:

<textarea style="width:100%; height:50%; resize:none; rows="5" cols="40">
<?php 
$output = implode("\n" , $QueryArray);
echo $output;
?>
</textarea>

However the below query prints:

Array
Array
Array
Array
..etc

Not sure where code is going wrong.

8
  • 1
    It's an array of arrays. Implode doesn't flatten it to be one dimensional. Commented Sep 28, 2015 at 19:37
  • 1
    how about implodeing: $QueryArray[] =$row["Column1"].' '.$row["Column2"].' '.$row["Column3"]); Commented Sep 28, 2015 at 19:39
  • @Dagon Yes this is the solution Commented Sep 28, 2015 at 19:45
  • @Dagon Yes this is the solution Commented Sep 28, 2015 at 19:46
  • @Dagon i cant reference myself can i Commented Sep 28, 2015 at 19:46

3 Answers 3

3

As you can see $QueryArray is an array of arrays

$QueryArray[] = Array($row["Column1"], $row["Column2"], $row["Column3"]);

You can access the $queryArray elements (array), in a foreach loop. Than use implode to print what you want.

<textarea style="width:100%; height:50%; resize:none; rows="5" cols="40">
<?php
$output = '';
foreach($array in $QueryArray) {
    $output .= implode("\n" , $array);
}
echo $output;
?>
</textarea>

I hope it helps

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

3 Comments

You're right @Dagon. My solution is only a dirty fix. The better way is to create a mono dimensional array, concatenating resulting rows with a "\n", in that way: $row["Column1"]. "\n". $row["Column2"]. "\n" .$row["Column3"]
quick? this is a slower fix
@nessuno I prefer simple over complex, which is what Dagon provided, thanks anyway
0

In your code $QueryArray is a array of array. if your field count are know, simply you can:

<?php 
$sql = $myquery;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
   while($row = $result->fetch_assoc()) {
      $QueryArray[] = $row["Column1"];
      $QueryArray[] = $row["Column2"];
      $QueryArray[] = $row["Column3"];
}
} else {}
?>

Comments

0

This is an old question but someone might need the answer.

echo "<pre>";
print_r($QueryArray);
echo "</pre>";

1 Comment

Is this actually the answer to how to implode a string?

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.