views:

217

answers:

3

Hello all,

I am trying to save lots of variables to a database and its getting ridiculous now. I am using PHP and MySQL.

Is there a way, I can get the array value and the array keys (array keys are exactly saqme as the table column/field names) in one go without having to add a new variable and table column pair.

To be honest, I just need help with constructing the SQL String only, the rest is setup for me.

I can add the new column to the database table when I have to store a new variable.

Thanks all for any help

A: 

You could try storing a serialized array of options in the database. for example, from: http://us3.php.net/manual/en/function.serialize.php.

<?php
// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.

$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn,
      "UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!odbc_execute($stmt, $sqldata)) {
    $stmt = odbc_prepare($conn,
     "INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!odbc_execute($stmt, $sqldata)) {
        /* Something went wrong.. */
    }
}
?>
bkildow
+3  A: 

If you want to create a SQL query from your array, this might help:

// Sample array
$array = array(
             'key1' => 'value1',
             'key2' => 'value2'
             ...
             'key10' => 'value10'
         );

// Get and escape the keys
$keys = array_map('mysql_real_escape_string', array_keys($array));
// Escape the values
$array = array_map('mysql_real_escape_string', $array);
// Build query
$query = "INSERT INTO table(`".implode('`, `', $keys)."`) VALUES('".implode("', '", $array)."')";

mysql_query($query);

In this case, the query would look something like this:

INSERT INTO
    table(`key1`, `key2` ... `key10`)
VALUES
    ('value1', 'value2' ... 'value10')

If you have a multidimensional array (an array of arrays) you can create a query as follows:

// Sample multidimensional array
$array = array(
             array('key1' => 'value1', 'key2' => 'value2'),
             array('key1' => 'value3', 'key2' => 'value4'),
             array('key1' => 'value5', 'key2' => 'value6')
         );

// Get and escape the keys
$keys = array_map('mysql_real_escape_string', array_keys(current($array)));
// Array to store values for the query
$values = array();
// Loop every row and insert into $values array
foreach($array as $row) {
    // Escape all items
    array_map('mysql_real_escape_string', $row);
    $values[] = "('".implode("', '", $row)."')";
}

$query = "INSERT INTO table(`".implode('`, `', $keys)."`) VALUES ".implode(', ', $values);

mysql_query($query);

And in this case, the resulting query would be something like this:

INSERT INTO
    table(`key1`, `key2`)
VALUES
    ('value1', 'value2'),
    ('value3', 'value4'),
    ('value5', 'value6')

Now only thing you have to worry about is creating the corresponding columns to the database.

Tatu Ulmanen
Awesome! Thanks Tatu - thats exactly what I was looking for, I am just too nooby to pull it off myself! :)
Abs
+1  A: 

Serialize the data and write it to a single field in one table. Then to retrieve the data unserialize it and you are left with an array.

<?php
$array = array("hello", "world");
$serialized = serialize($array);

// -> Then write $serialized to database
?>

To retrieve

<?php      
// -> First Get $serialized from database

$array = unserialize($serialized);
?>
Chaim Chaikin
+1 I learned this trick very recently myself, quite handy.
Erik
I figured this method when I tried making a form that was able to be changed by the admin at any time. So this was the easiest method without messing with previous entries when the admin changed the form.
Chaim Chaikin