0

I am trying to run a stored procedure using PHP. I want to put the results of the query into a javascript object. I cannot find how to accomplish this. I have run this code but I get one of the results I am returning is undefined. Here is my code:

$result = mysql_query("call sp_getGenre()");
    if($result === FALSE){
            die(mysql_error());
        }
        while($row = mysql_fetch_array($result)){
    ?>
            <script type="text/javascript">
                console.log(<? $row['Type'] ?>);
                var genreObj = new Object();
                genreObj.name = "<? echo $row['Type'] ?>";
                genreObj.level = <? echo $row['Level'] ?>;
    <?
                $parentID = $row['PrevID'];
                if($parent == null){ $parent = "0"; }
    ?>
                genreObj.parent = <? $parent ?>;
                arrGenre.push(genreObj);
            </script>
    <?
        }
    ?>

I am fairly new to the PHP world but would greatly appreciate a point in the right direction. Thanks in advance.

2
  • Can you paste here the output, why you don't use echo or = to display variable content in genreObj.parent = <? $parent ?>; You can easily create the object using Ajax Commented Feb 9, 2013 at 1:31
  • Thank you for teh fast replies. I'm not fully understanding. Can you give me an example of what genreObj.name = "<? echo $row['Type'] ?>"; should look like please. Commented Feb 9, 2013 at 1:34

3 Answers 3

3

Build an associative array in PHP resembling the JavaScript object you want, then use json_encode() to convert it into a JSON object, you can then easily pass on to JavaScript.

You may even be able to do something like:

<?php
// stuff
$resultObj = json_encode($row);
?>

<script type="text/javascript">
var genreObj = <?= $resultObj ?>;
</script>

This is not supposed to be a perfect solution to your problem, but hopefully it will give you some ideas.

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

3 Comments

Is it possible to build a PHP object and pass that object to a javascript object?
Not by default, your object class has to provide encoding methods. See stackoverflow.com/questions/4697656/…
Is using that method the only way?
1

I think

<? $row['Type'] ?>

Should be

<?= $row['Type'] ?> or
<? echo $row['Type'] ?>

Same goes for

<? $parent ?>;

4 Comments

I added the echo and still get no results. Firebug says one of the first returned value "bob" is undefined. genreObj.name = "<? echo $row['Type'] ?>";
You sure all of your PHP variables have values?
yes, I ran the stored procedure in HeidiSQL and saw all the results.
This did the trick I had to get rid of echo and just use ?= Thanks for the help.
0

Code below is not fully tested but the basic Idea is to Write all values into an array, then convert that array to a json object. From there assign the json object to the jsonresults variable. Now you can pull up all the info you need via javascript.

<?php


$result = mysql_query("call sp_getGenre()");
if($result === FALSE){
    die(mysql_error());
}
while($row = mysql_fetch_array($result)){
    $resultsarray = array("type"=> $row['type'], "level" => $row['Level'}, "previd" => $row['PrevID']);
    $jsonifiedresults = json_encode($resultsarray);
    ?>
            <script type="text/javascript">
                jsonresult = <? echo $jsonifiedresults; ?>;

                console.log(jsonresult.type);
                var genreObj = new Object();
                genreObj.name = jsonresult.type;
                genreObj.level = jsonresult.level;
                genreObj.parent = jsonreult.prevID;
                arrGenre.push(genreObj);
            </script>
<?php
        }
?>

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.