4

Been lurking on Stackoverflow for a long time but this is my first post. I am receiving a error related to displaying an array which is supposed to be populated by a mysql query. The echo function just returns ArrayArrayArray instead of what is supposed to be there. The mysql query is comparing a form input (the variable $data) .

<?php
$data = $_POST["search"];
global $data;

// Create Connection
$con = mysqli_connect(xxxxx,xxxxxx,xxxxx,xxxxx);

// Check Connection
if (mysqli_errno($con))

{
    echo "Failed To Connect To The Database" ;
}


//Perform Query To Compare And Return Results
$result_array = array();
$query = " SELECT url FROM data WHERE url LIKE '%$data%' "  ;
$result = mysqli_query($con, $query);
// While Loop To Return All Comparable Results
    while ($row = mysqli_fetch_array($result)) {

 $result_array[] = $row['url'];

 echo $result_array ;


}
?>
4
  • 1
    use should use print_r($result_array) or var_dump($result_array) Commented Feb 10, 2014 at 15:43
  • 1
    not sure what your question is but to print an array use print_r($result_array) Commented Feb 10, 2014 at 15:43
  • It is not an error. It's just the way arrays are printed using the echo command. You should use something else (as suggested in the other comments) to print the contents of an array. Commented Feb 10, 2014 at 15:45
  • When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation to accomplish this because you will probably create severe SQL injection bugs. Commented Feb 10, 2014 at 17:23

5 Answers 5

10

echo will print a string, try using something like print_r() or var_dump() instead

Example

echo '<pre>';
print_r($result_array);
echo '</pre>';

<pre> will allow for easier reading of the array

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

3 Comments

Thanks for the help, print_r() and var_dump() do work, but I require a array because another piece of code has to use the results of the array.
Then just use the $result_array in that piece of code.
By any chance are you returning this array to another file, over something like AJAX? If so, why not use echo json_encode($result_array);
3

You can try with following code.

<?php echo '<pre>'; print_r($result_array); echo '</pre>'; ?>

Comments

1

In your code, you want to replace echo $result_array; by echo $row['url']; (to display the content of url at each loop), or remove that line and add a print_r($result_array); after the while{} loop, to display all in one command.

Comments

1

It's also useful use echo implode(';', $result_array); for joining strings of each element in $result_array

Comments

1

echo posts the string version of your variable, which in this case will look like Array.

You can use var_dump($result_array); or print_r($result_array); to get the results printed correctly.

Comments

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.