0

I am building a simple report that I want to link to google's api to generate a line graph with the data, I am rather new to php and I cannot seem to get my data into json format. According the google's documentation the json format needs to be

"cols": [
    {"id":"","label":"Topping","pattern":"","type":"string"},
    {"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [
    {"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
    {"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
    {"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
    {"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
    {"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
]
}

I have built the query and tested this by its self and its works fine, my problem is building an array so that I can generate the correct json code. The code I am using to build this array and encode into json is:

function graphdata()
{
$array['cols'][] = array(
'id' => '',
'label' => 'DateCreated',
'pattern' => '',
'type' => 'string'
);
$array['cols'][] = array(
'id' => '',
'label' => 'UnitPrice',
'pattern' => '',
'type' => 'number'
);

$result = sqlsrv_query($conn, $query);
while($row = sqlsrv_fetch_object($result)){ 
$array['rows'][] = array (
'c' =>array(
array ('v' => $row->DateCreated->format('d-m-Y'), 'f' => ''),
array ('v' => $row->UnitPrice, 'f' => ''),
)
);
}
return $array;
}

print json_encode(graphdata());

Can anyone help with building this array, it doesn't seem to work at all but as far as I can see there is nothing that jumps out as being wrong, but saying that I am very new to php and no doubt i've done it wrong and it is a easy fix.

As a side note, I am using MSSQL.

More info following putting error reporting on:

Notice: Undefined variable: sqlsrv_query() expects parameter 1 to be resource

5
  • And what is the output of your print json_encode(graphdata());
    – vaso123
    Dec 10, 2014 at 15:14
  • It doesn't output anything I have just uploaded a new version with error reporting I've just updated my original post with the error I am receiving
    – Kris
    Dec 10, 2014 at 15:19
  • Then, I think your connection to the database failed. var_dump($conn)
    – vaso123
    Dec 10, 2014 at 15:22
  • No the connection was established I thought that first of all
    – Kris
    Dec 10, 2014 at 15:23
  • But your error says this: sqlsrv_query() expects parameter 1 to be resource. PHP is not lying, so the first parameter is not a resource.
    – vaso123
    Dec 10, 2014 at 15:31

1 Answer 1

1

Your database queries are not working because the $conn variable is not defined in the scope of the function.

To solve that, declare it as global:

global $conn;

or add it to your function as an argument:

function graphdate($conn)

The same goes for your $query variable.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.