programmer guy
programmer guy

Reputation: 133

An array of MySQL results...

What am I doing wrong here?

I am attempting to return a json object and I can't seem to get past the array... I've built hundreds of regular array and returned them as a json object but I am having a hard time wrapping my head around this one.

$rows = array();
$post_array = array();

$i = 0;

$result = mysql_query(" SELECT * FROM forum_posts WHERE permalink = '$permalink' AND LOWER(raw_text) LIKE '%$str%' " );


while($row = mysql_fetch_assoc($result))
    {
        $post_array[$i] = $rows[  "id" => htmlentities($row["id"]),
                                        "post_content" => htmlentities($row["content"]),
                                        "author" => $row["author"],
                                        "last_updated" => $row["last_updated"],
                                        "author_id" => $row["author_id"],
                                        "editing_author" => $row["editing_author"],
                                        "date" => $outputQuoteDate  ];
        $i++;
    }

Upvotes: 0

Views: 121

Answers (2)

Wiseguy
Wiseguy

Reputation: 20873

It looks like you mean to define an array for $post_array[$i] = .... Like this?

$post_array[$i] = array(
                      "id" => htmlentities($row["id"]),
                      "post_content" => htmlentities($row["content"]),
                      "author" => $row["author"],
                      "last_updated" => $row["last_updated"],
                      "author_id" => $row["author_id"],
                      "editing_author" => $row["editing_author"],
                      "date" => $outputQuoteDate,
                  );

(Also, I just took the liberty to respace that a little for readability.)

To convert your array to JSON, pass it to json_encode().

Update: Oh, before you ask about it, I just noticed I added a comma out of habit after the last item in the array. It looks like it's out of place, but it's actually fine to have it there when defining arrays. It doesn't serve any special purpose, but it allows you to copy/paste/remove lines from the array without having to worry about whether or not to add/remove a trailing comma.


As an aside, you don't have to manually increment a numeric array index $i. If you just do this:

$post_array[] = array(...);

it will automatically assign the next available numeric index.

Upvotes: 2

Code Magician
Code Magician

Reputation: 24032

Do you mean do be doing something like this:

$post_array = array();

$i = 0;

$result = mysql_query(" SELECT * FROM forum_posts WHERE permalink = '$permalink' AND LOWER(raw_text) LIKE '%$str%' " );


while($row = mysql_fetch_assoc($result))
    {
        $post_array[$i] =array(  "id" => htmlentities($row["id"]),
                                        "post_content" => htmlentities($row["content"]),
                                        "author" => $row["author"],
                                        "last_updated" => $row["last_updated"],
                                        "author_id" => $row["author_id"],
                                        "editing_author" => $row["editing_author"],
                                        "date" => $outputQuoteDate  );
        $i++;
    }

Then you can simply use json_encode to encode your array as json.

e.g.

echo json_encode($post_array);

You can't build an array the way you were with $rows[...], you need to use array. Also, instead of managing the ordinals for your array, you can just use array_push

Upvotes: 1

Related Questions