2

I am just starting on the PHP. I am working on getting information from WordPress database.

The plugin writes data to a DB, from the Sign up form.

What I want is to get this data formatted on my own way, let's say a table, on a separate page.

So what I did already, is to connect to a DB, and print the data. Did it by this:

<?php

    //connect to the database
    mysql_connect ("host","user","pasw") or die ('Cannot connect to MySQL: ' . mysql_error());
    mysql_select_db ("database") or die ('Cannot connect to the database: ' . mysql_error());

    //query
    $query = mysql_query("select id, data from wp_ninja_forms_subs") or die ('Query is invalid: ' . mysql_error());

    //write the results

    while ($row = mysql_fetch_array($query)) {
        echo $row['id'] . " " . $row['data'] . "
    ";

    // close the loop
    }

    ?>

The thing is, that I get the results, which doesn't really suit me:

14 a:4:{i:0;a:2:{s:8:"field_id";i:2;s:10:"user_value";s:8:"John Doe";}i:1;a:2:{s:8:"field_id";i:4;s:10:"user_value";s:11:"+3706555213";}i:2;a:2:{s:8:"field_id";i:12;s:10:"user_value";s:9:"Company 1";}i:3;a:2:{s:8:"field_id";i:8;s:10:"user_value";a:1:{i:0;s:13:" Finansiniai ";}}} 15 a:4:{i:0;a:2:{s:8:"field_id";i:2;s:10:"user_value";s:10:"Bill Gates";}i:1;a:2:{s:8:"field_id";i:4;s:10:"user_value";s:11:"+5654412213";}i:2;a:2:{s:8:"field_id";i:12;s:10:"user_value";s:9:"Company 2";}i:3;a:2:{s:8:"field_id";i:8;s:10:"user_value";a:1:{i:0;s:13:" ?vaizd˛io ";}}} 16 a:4:{i:0;a:2:{s:8:"field_id";i:2;s:10:"user_value";s:7:"Person3";}i:1;a:2:{s:8:"field_id";i:4;s:10:"user_value";s:7:"6463213";}i:2;a:2:{s:8:"field_id";i:12;s:10:"user_value";s:9:"Company 3";}i:3;a:2:{s:8:"field_id";i:8;s:10:"user_value";a:2:{i:0;s:10:" HTML/CSS ";i:1;s:12:" Photoshop ";}}} 17 a:4:{i:0;a:2:{s:8:"field_id";i:2;s:10:"user_value";s:11:"Pretty Girl";}i:1;a:2:{s:8:"field_id";i:4;s:10:"user_value";s:9:"643122131";}i:2;a:2:{s:8:"field_id";i:12;s:10:"user_value";s:4:"Zara";}i:3;a:2:{s:8:"field_id";i:8;s:10:"user_value";a:1:{i:0;s:13:" ?vaizd˛io ";}}} 

Now, what I want to see is a table:


2013.10.25    Pretty Girl 643122131   Zara         Įvaizdžio 
2013.10.25    Person3     6463213     Company 3    HTML/CSS ,  Photoshop 
2013.10.25    Bill Gates  +5654412213 Company 2    Įvaizdžio 
2013.10.25    John Doe    +3706555213 Company 1    Finansiniai

Could someone tell me, how to achieve that? I believe, that my data output is an array, or am I wrong about it too?

If yes, maybe someone could give me an example how to format one part of that array, so I could do the rest?

Or even some hint on what to google for?

Thanks!

Sruit A.Suk
  • 7,073
  • 7
  • 61
  • 71
user2917823
  • 199
  • 1
  • 9
  • 1
    hello, first at all, it looks like a serialized string, no an array, an dprobably teh result is ok according the form that you are quering , and my question is, why you are using mysql functions instead wp db api? if you build a plugin, you wiull need to use wp db api – Carlos Oct 24 '13 at 22:44
  • `unserialize($row['data']); ` and mb y, try to use wp api, it easier and more useful. – DaunnC Oct 24 '13 at 23:02
  • `unserialize()` will decode string back to php form, but formatting it into a `` is up to you. Do a google search for `php format mysql to table` or similar. A
    – gwillie Oct 24 '13 at 23:31
  • Thanx for the comment. I am not using WP api, because I am using this on seperate site, to connect and take data from WP DB elsewhere. I do not know, I think I just found a solution for mySQL first, instead of WP API. – user2917823 Oct 25 '13 at 15:49

1 Answers1

0

Use "\n" for new line character :)

Your individual values (a:4:{i:0....}) have been "serialized". This one was an array of four elements that was passed to the serialize() PHP function. The function returned it's textual representation (i.e. it has "serialized" the array). So you have the entire array saved in one cell in database as simple text.

Function unserialize() does the opposite - turns the "serialized" (text) values and returns the original PHP object (an array in this case).

You can serialize almost any PHP object as long as it doesn't have some any "resources" attached to it. But there is already a separate question for that: What could cause a failure in PHP serialize function? As long as you serialize arrays of numbers, strings and arrays and even simple objects there is nothing to worry about.

Your first cell (no 14) when unserilalized:

array (
  0 => 
  array (
    'field_id' => 2,
    'user_value' => 'John Doe',
  ),
  1 => 
  array (
    'field_id' => 4,
    'user_value' => '+3706555213',
  ),
  2 => 
  array (
    'field_id' => 12,
    'user_value' => 'Company 1',
  ),
  3 => 
  array (
    'field_id' => 8,
    'user_value' => 
    array (
      0 => ' Finansiniai ',
    ),
  ),
)

(Using: http://www.functions-online.com/unserialize.html) Run these trough a for loop or something to get the rendering you need, that's up to you.

Community
  • 1
  • 1
frnhr
  • 12,354
  • 9
  • 63
  • 90