0

Here's my situation:

I have a MYSQL database with 2 fields: 'ID' and 'string'. The 'string' field stores strings in form of serialized arrays. So, to extract it back, I use this PHP code:

$rez=mysql_query("SELECT * FROM drawings") or die(mysql_error());

$array=array();

while($row=mysql_fetch_array($rez))
{
    $array[]=unserialize($row['string']);
}

Now my $array variable should contain an array of arrays, right? Assuming I did that correctly, I then echo $array; so my ajax call catches it inside the return_data, like this:

var return_data = hr.responseText;

Just to test if I can extract a value, I then tried implementing the following code, but it doesn't seem to work:

var arr = return_data.split(",");
var sub_arr = arr[0].split(",");
alert(sub_arr[0]);

What am I doing wrong?

Additional info:

I am basically storing a bunch of coordinates inside the MYSQL database, each row being a separate array of coordinates, for example (12,13,14,16,17,20) - these would be 3 "points" contained in one row.

Then I'm using an ajax call to extract all the records from the database in a form of array, which contains arrays of multiple numbers (I know that each 2 neighboring numbers make up one point).

EDIT:

The chrome javascript console outputs:

Error in event handler for 'undefined': Cannot read property 'disable_serps_pos_numbers' of undefined TypeError: Cannot read property 'disable_serps_pos_numbers' of undefined at BasePlugin.GooglePlugin.getSomeCorePrefs (chrome-extension://akdgnmcogleenhbclghghlkkdndkjdjc/content_scripts/google.js:129:48) at chrome-extension://akdgnmcogleenhbclghghlkkdndkjdjc/lib/lib.js:44:25 at miscellaneous_bindings:287:9 at Event.dispatchToListener (event_bindings:356:21) at Event.dispatch_ (event_bindings:342:27) at Event.dispatch (event_bindings:362:17) at Object.chromeHidden.Port.dispatchOnMessage (miscellaneous_bindings:253:22)

9
  • can you please give us the output of console.log(return_data) ? you will see it in the google chrome javascript console.
    – Gnagno
    Commented Aug 14, 2013 at 15:24
  • Gnagno is right, we need to see what is delivered back.
    – mvw
    Commented Aug 14, 2013 at 15:28
  • 1
    1) Why are you storing serialized data in a database? 2) You cannot echo an array because what will be displayed will be Array (this is easy to see when debugging the request/response pair) 3) ever heard of JSON?
    – PeeHaa
    Commented Aug 14, 2013 at 15:28
  • From the code you posted, you have an array of arrays assuming the 'string' column in your database contains a serialized array. But an array is not a comma delimited string. In order to split the return data by commas, you'd want to do something like $array[] = implode(",", unserialize($row['string'])); Commented Aug 14, 2013 at 15:29
  • I added an "EDIT" where you can see my console output. PeeHaa, I thought that is the best way to store an array inside a mysql database.
    – Zannix
    Commented Aug 14, 2013 at 15:29

3 Answers 3

3
while($row=mysql_fetch_array($rez))
{
    $array[]=unserialize($row['string']);
}

echo json_encode($array);

Then in the Javascript code (assuming jQuery):

$.parseJSON(return_data)

Then you can use it like a 2D array in Javascript.

1
  • This does seem to be exactly what I need. Unfortunately I'm not using jquery because I never learned it. Instead I'm relying on XMLHttpRequest(); - Would you be so kind to post the code I would need to get the data from my php file using jquery ajax method?
    – Zannix
    Commented Aug 14, 2013 at 15:41
1

Use the standard functions that come with jQuery and PHP to convert arrays into JSON data and back to array.

Like json_encode() and json_decode() in PHP

and JS: Parse JSON in JavaScript?

0

First, I strongly recommend you switch to using PDO for SQL queries, but to answer your question you said that you are doing:

echo $array;

Which will result in an error Array to String Conversion.

I recommend using doing echo json_encode($array); which will convert the array to a json object.

http://php.net/manual/en/function.json-encode.php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.