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)

Zannix
  • 1,473
  • 3
  • 16
  • 26
  • can you please give us the output of console.log(return_data) ? you will see it in the google chrome javascript console. – Gnagno Aug 14 '13 at 15:24
  • Gnagno is right, we need to see what is delivered back. – mvw Aug 14 '13 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 Aug 14 '13 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']));` – Michael Wheeler Aug 14 '13 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 Aug 14 '13 at 15:29
  • If you need to store things like arrays on a DB and the only other field you have besides the serialized array is an id, instead of using mysql, better try with CouchDB. Really, mysql is not what you need here. (CouchDB stores data in json format) – chris-l Aug 14 '13 at 15:30
  • Wait, I missed this part: `echo $array;` prints 'Array' or something like that. It doesn't print a comma delimited string. One way would be `echo json_encode($array);` and then decode the JSON string with javascript. – Michael Wheeler Aug 14 '13 at 15:31
  • you're right Michael, it does seem to print "Array" because, the letter I get at the end is "A". So I would do a json_encode($array), then echo that and how would I go about it later? Sorry never worked with JSON – Zannix Aug 14 '13 at 15:34
  • You must put the contents of your array in the AJAX call in some usable format and have the appropriate code on the receiving page to parse it and convert back into an JavaScript array. Even CSV format might work, but using JSON should be very easy, as the JSON format is well supported by PHP and JavaScript. – mvw Aug 14 '13 at 15:42

3 Answers3

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.

Michael Wheeler
  • 670
  • 7
  • 11
  • 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 Aug 14 '13 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?

Community
  • 1
  • 1
Jens
  • 174
  • 1
  • 6
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

tempcke
  • 700
  • 8
  • 15