Arrays in DB?????

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Arrays in DB?????

Post by Perfidus »

What happens if I write the following text in a database row call numbers:

"one", "two", "three", "four"

and after I try to recover it like this:

Code: Select all

$result= mysql_query("SELECT numbers FROM arrays" ,$link);
while ($row = mysql_fetch_array($result)){
$numbers=array($row['numbers']);
}
I thought I will recover an array, but it doesn't work.
Any hints?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

I have tried like this:

Code: Select all

$result= mysql_query("SELECT numbers FROM arrays" ,$link);
while ($row = mysql_fetch_array($result)){
$numbersbeta=unserialize($row['numbers']);
}
$numbers=array($numbersbeta);
But it was a mess too.
Is it the right way to use it?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

the content you are pulling from the database has to already be serialized.


1) when adding into the db serialize the content
2) when pulling from the db unserialize the content
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

Yeah!, That worked out for me!

New question master:

Code: Select all

array_push($cities, "$newcity");
sort($cities);
$clave = array_search('$newcity', $cities);
echo $clave."<br>";
Why this is echoing nothing?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Where is $newcity coming from?

Code: Select all

echo $newcity;
to see if its the expected value
Post Reply