Jump to content

json_encode(); HALP!


RP

Recommended Posts

So i was working on this script last night and had it working fairly well, i came back in the morning and made some alts, only to have my computer crash and as per usual i had saved a unworking version and couldnt revert. My last version of the page was at 4pm, however its not working. It is constantly giving me an "Fatal error: Call to undefined function:  json_encode() on line 64". Last night it was delivering me a Json array like it should be.

 

<?php 
$code = "user 2";

if (!$db = mysql_connect($host, $user, $pass)) {
echo 'Could not connect to mysql';
exit;
}
if (!mysql_select_db($dbname, $db)) {
echo 'Could not select database';
exit;
}



$query = "SELECT * FROM graph WHERE username = '$code'";
$result = mysql_query($query);
$num = mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {
$value0=mysql_result($result,$i,"value0");
$value1=mysql_result($result,$i,"value1");
$value2=mysql_result($result,$i,"value2");
$value3=mysql_result($result,$i,"value3");
$value4=mysql_result($result,$i,"value4");
$i++;
}

$dataset = array(
        'ds1'=>array(
		   array(0,$value0),
               array(1,$value1),
               array(2,$value1),
               array(3,$value3),
               array(4,$value4)
        )
);

$options = array(
        'xTicks'=>array(
                array('v'=>0,'label'=>'Mon'),
                array('v'=>1,'label'=>'Tue'),
                array('v'=>2,'label'=>'Wed'),
                array('v'=>3,'label'=>'Thur'),
			array('v'=>4,'label'=>'Fri')
        )
);

$return = array(
        'dataset'=>$dataset,
        'options'=>$options
);


header("Content-type: text/json");
echo json_encode($return);
?>

 

Doing some reaserach i tried using the default php example for json_encode();

<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

echo json_encode($arr);
?>

 

this gave me a fatal error as well. Nothing has changed on my server at all.

 

So i ask you PhpFreaks.com, WTF is going on? and furthermore, how do i fix it?

Link to comment
Share on other sites

If it is telling you Call To Undefined Function, then that means that the function is not defined. Where is this json_encode()  function actually set at?

 

You need to ensure that if it is in an external file like an include, that the include is being well ... included.

 

anytime you get a Call To Undefined Function, that means that the function is not available to that part of the script.

 

If xyz(); is defined in included_stuff.php this would return an error

 

xyz('pass_var');

include('included_stuff.php');

 

Here I am calling the function before it is available in the script as it is defined in included_stuff.php

 

Find the function and make sure it's defined before calling it

 

nate

Link to comment
Share on other sites

The Dataset links into this piece of code:

<script>

 

 

Object.extend(Plotr.LineChart.prototype,{

        getRemoteDatasetAndRender: function(url) {

                var xhr_options = {

                        method: 'get'                               

                }

                xhr_options.onComplete = function(xhr) {

                        var json = eval('('+xhr.responseText+')');

                        this.addDataset(json.dataset);

                        this.setOptions(Object.extend(this.options,json.options));

                        this.render();

                }.bind(this);

                new Ajax.Request(url,xhr_options);               

        }       

});

//we won't include the axis details in the options,

// as the remote call will provide them

var options = {

        padding: {left: 30, right: 0, top: 10, bottom: 30},

        backgroundColor: '#dbdbdb',

        colorScheme: 'red'

};

var line = new Plotr.LineChart('plotr',options);

//then just pass the URL to the function

line.getRemoteDatasetAndRender('dataset4pm.php');

 

 

</script>

 

Would this undefiend function apply even for the default example i listed in my first post?

Link to comment
Share on other sites

I havent switched server or touched anything, i almost wish i had so i could find the problem.

 

Running phpinfo() and doing a find for json came up with only one result under Configure Command;

 

'--with-zip=/usr' '--with-json' '--with-pear' '--enable-utf8' '--enable-track-vars' '--enable-trans-sid'

 

Im not sure exactly what this means but i do know that i was perviously able to watch firebug display my Json string and in turn watch it build my graph.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.