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);

4 Answers 4

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);

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

5 Comments

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?
I need more information. To select a specific value you can do like this : $value = $data[0]['Label']; or $value = $data[1]['Status'];
Here's the link. I just wanted to how to pass that two numbers 1 and 3 into that jquery sparkline. I need to do like this? <?php $value?>
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
can you mark this post as solved ? we will discuss in the other one.
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;
}

2 Comments

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?
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 = <?php echo json_encode($data); ?> (This code hase to be in your JS block)
0

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

Comments

0

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

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.