Log in

View Full Version : How to get data from table database? i forget



smansakra
08-25-2011, 01:05 AM
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


$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

xtiano77
08-25-2011, 02:26 AM
Is this what you are looking for?



$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;
}

smansakra
08-25-2011, 03:08 AM
Thanks for reply, unfortunately

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:

SELECT *
FROM
database.table

but then i don't know what to write. :'(

Brillig
08-25-2011, 07:17 PM
Your column names are dynamic?

You sure about that?

djr33
08-25-2011, 07:36 PM
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.

smansakra
08-25-2011, 08:55 PM
:'(
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

$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?? :'(

smansakra
08-25-2011, 09:00 PM
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


$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'
);
*/