0

I've successfully connected to a PostgreSQL database and even the query is running successfully.

But when I try to print the data using this code echo $data I get error as Array to String Conversion.

Tried searching in the forum and google. Nothing was fruitful.

Please help me.

Code used to convert it to array and print it.

if ( ! $myquery ) {
    echo pg_error();
    die;
}

$data = array();
for ($x = 0; $x < pg_num_rows($myquery); $x++) {
    $data[] = pg_fetch_assoc($myquery);
}
// echo json_encode($data);

// $data2 = array_shift($data);

echo $data;

pg_close($server);
Unknown User
  • 3,598
  • 9
  • 43
  • 81

4 Answers4

1

Read the error :

Array to String Conversion

Try to display it using var_dump or print_r because it's an array. The error says that you want to display an array with echo which can only display string.

exemple : var_dump($data);

Tosx
  • 606
  • 3
  • 8
  • `array(2) { [0]=> array(2) { ["Label"]=> string(3) "One" ["Count"]=> string(1) "1" } [1]=> array(2) { ["Status"]=> string(7) "Two" ["Count"]=> string(1) "3" } }` - I got a result like this. Can you please say me now how will I select pass this value to a `jquery` sparkline chart? – Unknown User Dec 31 '13 at 09:57
  • I need more information. To select a specific value you can do like this : $value = $data[0]['Label']; or $value = $data[1]['Status']; – Tosx Dec 31 '13 at 10:01
  • Here's the [**link**](http://stackoverflow.com/questions/20852571/passing-database-query-results-to-jquery-sparkline-charts-using-php). I just wanted to how to pass that two numbers `1` and `3` into that jquery sparkline. I need to do like this? `` – Unknown User Dec 31 '13 at 10:02
  • I'm not sure but i think you will have to print you value in html balise (hidden filed for exemple) and retrieve it using javascript : document.getElementById – Tosx Dec 31 '13 at 10:05
  • can you mark this post as solved ? we will discuss in the other one. – Tosx Dec 31 '13 at 10:11
1

To just print the array (for debuging) use var_dump() like this:

var_dump($data)

But if you want to echo every line in the array you have to loop:

foreach ($data as $row) {
    echo $row;
}
Matthias Dunkel
  • 305
  • 2
  • 14
  • `array(2) { [0]=> array(2) { ["Label"]=> string(3) "One" ["Count"]=> string(1) "1" } [1]=> array(2) { ["Status"]=> string(7) "Two" ["Count"]=> string(1) "3" } }` - I got a result like this. Can you please say me now how will I select pass this value to a `jquery` sparkline chart? – Unknown User Dec 31 '13 at 09:57
  • The function `var_dump($data)` is only for debuging. Don't use this for actual coding... I don't know jQuery very well, but you could try to pass it to a JS variable like this `var data = ` (This code hase to be in your JS block) – Matthias Dunkel Dec 31 '13 at 10:16
0

You are pinting array with echo.Try print_r(); that will print array. Like this print_r($data);

Mahmood Rehman
  • 4,303
  • 7
  • 39
  • 76
0

Try using var_dump($var) or print_r($var) functions

Adarsh Nahar
  • 319
  • 3
  • 10