www.webdeveloper.com
Results 1 to 6 of 6

Thread: how to put data from database directly to array?

  1. #1
    Join Date
    Mar 2007
    Posts
    101

    Question how to put data from database directly to array?

    how to put data from database directly to array? for example i will get a data from my database like this "a,b,c,d" is there a way of putting the data in the array without looping? because what i did is i use substr() function and one by one i put it to array and the " , " is my sign that the data should be put to array.

    just like this

    Code:
    $string="a,b,c,d,";
     $arrCtr=0;
    for ($ctr=0; $ctr<=strlen($string); ++$ctr)
    {
      $tmpStorage=substr($string,$ctr,1);
      if ( $tmpStorage!=",")
      {
        $ExplodedString=$ExplodedString . $tmpStorage;
      }
      else
      {
        $array[$arrCtr]=$ExplodedString;
        $ExplodedString="";
        $arrCtr+=1;
      }
    }
    output
    =>a for $array[0]
    =>b for $array[1]
    =>c for $array[2]
    =>d for $array[3]


    if there any other way to do this that will make the program more faster, because my program is more on parsings. pls share your knowledge. thanks in advance...

  2. #2
    Join Date
    Dec 2007
    Location
    Swindon, England.
    Posts
    37
    Umm if you use a MySQL database you can utilize functions like mysql_fetch_row...

    So all you would do is have:

    Code:
    $query = "SELECT * FROM table";
    $result = mysql_query($query);
    $array = mysql_fetch_row($result);
    print_r($array);
    print_r will show you your array...

  3. #3
    Join Date
    Mar 2007
    Posts
    101
    thanks for reply.. yes im using mysql and that how i also query my data from database.. what i mean is.. for example the data in my data base is like this " a,b,c,d " and that data is only in one field or line of database and when i get that data from database it should directly put or store in array.. print_r is just like print right? and when i get have it in array that data can be use many times inside the program...

  4. #4
    Join Date
    Dec 2007
    Location
    Swindon, England.
    Posts
    37
    oh okay I see what you mean.... your data is stored as "a,b,c,d" in the database field.

    you can just use explode I think?

    PHP Code:
    $dataField "a,b,c,d";
    $array explode(","$dataField); 
    Then you can see the content of the array using print_r again - it just allows you to look at the content of the array (for debugging really) and then you can access it like $array[0], $array[1] etc.

    Is this making sense to you?
    Last edited by smickus; 12-30-2007 at 11:32 AM. Reason: error in code!

  5. #5
    Join Date
    Mar 2007
    Posts
    101
    yeah.. thats what i want.. thank you very much smickus.. this is just a simply function but its really help me a lot and work my system fast... thanks again...

  6. #6
    Join Date
    Dec 2007
    Location
    Swindon, England.
    Posts
    37
    yeah explode is very handy! good luck with it...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles