homepage Welcome to WebmasterWorld Guest from 54.197.15.196
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
Copy value from array into new field?
MrFox



 
Msg#: 4071001 posted 2:34 pm on Jan 30, 2010 (gmt 0)

First post here while trying to learn MySQL and PHP. I am reading some books about all this and in the meantime working on my website trying to change some things.

I am kind of stuck at the moment. I have this field with the following arrays in it:
a:4:{i:0;s:33:"http://giganarma.googlepages.com/";i:1;s:67:"It adds a camera function that can be freely moved in Armed Assault";i:2;s:0:"";i:3;s:0:"";}

Sometimes there is a link in it and sometimes there is not and I want to copy that link when present from all arrays in that field to a new field which will only contain the link and not the other info.

The thing is, I would like it if I can get some help with how to use the select command to copy the link from the above into a new field.
I am using this command till now to copy things over:
SELECT row_1 FROM table FOR UPDATE;
UPDATE table SET row_2 = row_1;

I hope someone can help me with this.
Thanks in advance.

 

MrFox



 
Msg#: 4071001 posted 4:39 pm on Jan 30, 2010 (gmt 0)

Sorry, I am not sure how I can edit my post. But, I think those are strings inside a string, not array's right?

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4071001 posted 7:51 pm on Jan 30, 2010 (gmt 0)

Welcome aboard MrFox.

When you store that "array" into a database field, yes, it's now a string. When you extract it, it's still a string - you may be able to use eval() on it, something like

$var = Array(eval($row['field']));

Don't even know if that would work. Alternatively you'd split/explode it and get it into an array that way.

If you wish to just extract the URL/link in a select, I don't know an easy way to do that - you may be able to cobble together something by combining regex and substring, but it would be that, cobbled, and probably complex.

I think the problem here is that this kind of thinking - storing a group of values as a delimited list in a varchar or text field - is bereft with problems, what you're seeing among them: the difficulty in getting at sub-parts of the data.

This

a:4:{i:0;s:33:"http://example.googlepages.com/";i:1;s:67:"It adds a camera function that can be freely moved in Armed Assault";i:2;s:0:"";i:3;s:0:"";}

should really be structured in a set of fields, with each member either in a single row or across relational tables, depending on how it got there in the first place.

So really, if you don't want to do that, the best thing is to probably explode/split on the colons and look for the member in the nth place where you expect it to be.

MrFox



 
Msg#: 4071001 posted 9:33 pm on Jan 30, 2010 (gmt 0)

Thanks a lot for the reply, I have been trying some different things you said already but not succesfull yet. But I will keep trying.

I fully agree this way is not the most optimum way to store data but it is/was out of my control as this is done by a plugin I used on my website. I want to seperate those values now and give each its own field (as you also suggested), thats why I need to get it out of the string.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4071001 posted 3:36 am on Jan 31, 2010 (gmt 0)

Ah! That changes things. What is the significance of { } in the string?

$all = $row['whatever'];

$pre = preg_replace('/\{[^\}]+\}/','',$all); // gives you a:4

$within = explode('/:/',preg_replace('/[^\{]+\{([^\}]+)\}/',"$1",$all)); // should give you an array of everything in { }

So you explode $pre in two, and the rest is already in an array . . . regexp might be faulty, getting a bit tired and cross eyed here . . .

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved