
|
View Full Version : PHP Arrays from SQL fields.
horizon 07-19-2006, 01:36 PM Hi,
I'm trying to select an SQL field which contains arrays.
Under PHP, will all know that the following codes could be done like this:
<?php
$arrays = array("1", "2", "3");
foreach ($arrays as $array) {
echo "$array<br />";
}
?>
Then, all the arrays will be shown one after another (with <br />).
Althought, when I try this from a mySQL field, it does not show them seperately but stays under one comma line. Is there anyone who could help me on that ?
I know that it must, typically, be done like this:
<?php
$arrays = array($myvariable['my_field_name']); // Supposing the mysql_fetch_array would be done above that line.
foreach ($arrays as $array) {
echo "$array<br />";
}
?>
But when I try, this one, it remains under one series of comma rather than seperately.
Any solutions ? :)
lets say that you're doing this:
$myData=mysql_fetch_array($query_result);
foreach($myData as $key=>$value)
echo "Field Name:".$key." = ".$value."<br />";
Wish this help
horizon 07-19-2006, 03:00 PM I wish I could say it works but this is was it returns unfortunitely:
Field Name:0 = 2,3
Any other solution ? :|
Burhan 07-19-2006, 03:19 PM I don't think you understand how arrays work. array($myvariable['my_field_name']) creates an array with only one element. array(1,2,3) is an array of three elements.
If your field from mysql contains something like this:
hello,there,everyone
And you want to print each word separately, then you need to do this:
$array = explode(",",$myvariable['my_field_name']);
foreach($array as $item) { echo $item.'<br />'; }
horizon 07-19-2006, 03:28 PM @fyrestrtr
You must be right. I did not think I understood the way array works until today since - it works !!!. Thanks very much for posting this. :tup .
creativeartist 07-21-2006, 10:03 PM $array = explode(",",$myvariable['my_field_name']);
foreach($array as $item) { echo $item.'<br />'; }
More than this it is better to use while than foreach loop.Normally foreach is used for a 2- D array
horizon 07-21-2006, 10:41 PM Even though it is working, would it be possible to post the while loop version ? In the mean time, if, according to your reply above, it is better to use a while loop statement, would it be possible to know which one uses less PHP ressources over server between these two ? ;)
brendandonhu 07-22-2006, 01:29 AM Foreach is generally faster than while loops.
NE-Andy 07-22-2006, 05:16 AM This is what you can do with arrays (something tells me this isn't excatly what OP wants, but oh well):
Insert record
$dataArray = array(1, 2, 3);
$query = "INSERT INTO table (`dataArray`) VALUE ( \"" . seralize($dataArray) . "\");";
$mysql_query($query);
Retrieve value
$query = "SELECT dataArray FROM table LIMIT 1;";
$result = mysql_query($query);
$data = mysql_fetch_row($result);
$dataArray = unserialize($data[0]);
horizon 07-22-2006, 09:42 AM A good thing would be to set a protection with SQL injection:
$dataArray = array(1, 2, 3);
$query = "INSERT INTO table (`dataArray`) VALUE ( \"" . seralize($dataArray) . "\");";
$mysql_query($query);
to read :
$dataArray = array(1, 2, 3);
if (is_array($dataArray)) {
$setdataArray = base64_encode(trim($dataArray));
$query = "INSERT INTO table (`dataArray`) VALUE ('$setdataArray')";
$mysql_query($query);
}
Then, this one:
$query = "SELECT dataArray FROM table LIMIT 1;";
$result = mysql_query($query);
$data = mysql_fetch_row($result);
$dataArray = unserialize($data[0])
Not much changes but:
$query = "SELECT dataArray FROM table LIMIT 1;";
$result = mysql_query($query);
$data = mysql_fetch_row($result);
$dataArray = base64_decode(trim(($data[0]));
However, this topic is about using arrays without depending on SQL statements. ;)
bigfan 07-22-2006, 07:34 PM ...it is better to use while than foreach loop.Normally foreach is used for a 2- D arrayThat's a crock.
|