Advanced Search

Results 1 to 7 of 7

Thread: How to get data from table database? i forget

  1. #1
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question How to get data from table database? i forget

    i forget how to get data from table database :'(
    below is my table

    options (in table options, i have 2 rows named with 'NAME' and 'VALUE' )
    +--------------------------------
    | NAME | VALUE
    |--------------------------------
    | site_name | Just My site
    | site_email | my@somedomain.com
    | site_description | no description here
    | site_owner | smansakra girl
    +--------------------------------------------

    i want to return the result to an array

    PHP Code:
    $site_details = array();
    //here will be the code to get data
    foreach( i forget what i should write here){
       
    //and i don't know how to use array_merge
    }

    actually i want the final result is like below
    $site_details 
    = array(
       
    'site_name' => 'Just My site',
       
    'site_email' => 'my@somedomain.com',
       
    'site_description' => 'no description here :)',
       
    'site_owner' => 'smansakra girl'
    ); 
    HELP, thanks
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  2. #2
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    Is this what you are looking for?

    PHP Code:
    $query "SELECT 
                    site_name, 
                    site_email, 
                    site_description, 
                    site_owner 
            FROM 
                    database.table 
            WHERE 
                    refine = :criteria 
            ORDER BY site_name ASC"
    ;
    $statement $db -> prepare($query);
    $statement -> bindParam(":criteria"$parameter);
    $statement -> execute();
    $site_details = array();
    foreach(
    $statement -> fetch(PDO::FETCH_ASSOC) as $key => $value){
        
    $site_details[$key] = $value;


  3. #3
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks for reply, unfortunately
    Code:
    site_name, 
    site_email, 
    site_description, 
    site_owner
    are dynamic, so when i use a form to add name and value, the name rows will be add.
    so, i think it better use:
    PHP Code:
    SELECT *
            
    FROM 
                    database
    .table 
    but then i don't know what to write. :'(
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  4. #4
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Your column names are dynamic?

    You sure about that?

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    I don't understand this either. I think you should read through a MySQL tutorial to remember all of the basics and then look at the problem again.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    :'(
    oKE, i will repeat it to make it more clear

    1.) i have a database table like below
    +--------------------------------
    | NAME | VALUE
    |--------------------------------
    | site_name | Just My site
    | site_email | my@somedomain.com
    | site_description | no description here
    | site_owner | smansakra girl
    +--------------------------------------------

    2. i have an system administration to submit a name and a value for it (look at the table, there are NAME and VALUE row)

    3. for example, on my admin page, i submit 'site_keyword' as NAME and 'reni, smansakra, indonesia' as VALUE
    so, the table will change like below

    +--------------------------------
    | NAME | VALUE
    |--------------------------------
    | site_name | Just My site
    | site_email | my@somedomain.com
    | site_description | no description here
    | site_owner | smansakra girl
    | site_keyword | reni, smansakra, indonesia
    +--------------------------------------------

    4. then i have an array, and i want each of the content of the table above become key and value,
    like below

    PHP Code:
    $site_details = array(
       
    'site_name' => 'Just My site',
       
    'site_email' => 'my@somedomain.com',
       
    'site_description' => 'no description here :)',
       
    'site_owner' => 'smansakra girl',
       
    'site_keyword' => 'reni, smansakra, indonesia',
    'site_anotherifadded' => 'valuethatadded'
    ); 
    5. anybody help?? :'(
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  7. #7
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    function get_from_options(){
            
    $table_name = array();
            
    $table_content = array();
            
            
    $this->load->database();
            
    $query $this->db->query("SELECT * FROM `options`");
            
    $row $query->row_array();
            foreach (
    $query->result_array() as $row)
            {
               
    array_push($table_name$row['name']);
               
    array_push($table_content$row['content']);
            }
            return 
    array_combine($table_name$table_content);

    for now, just the poor code above that i can create (using codeigniter)


    that code can result like what i want
    PHP Code:
    $site_details get_from_options()
    /*
    will result like below
    $site_details = array(
       'site_name' => 'Just My site',
       'site_email' => 'my@somedomain.com',
       'site_description' => 'no description here :)',
       'site_owner' => 'smansakra girl',
       'site_keyword' => 'reni, smansakra, indonesia',
    'site_anotherifadded' => 'valuethatadded'
    );  
    */ 
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

Bookmarks

Posting Permissions

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