LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-17-2012, 08:18 AM   #1
rm_-rf_windows
Member
 
Registered: Jun 2007
Location: Europe
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 23
PHP Array to Tuple: Is there a built-in php function that does this?


Hey all,

I have a php program, an unconventional research NLP program, and in my program it would be useful to take an array which contains all of the attributes of a specific table in the form of key (attribute names) value (attribute values). I'm using static classes and passing info around...

Does anybody know if it's possible, from such an array, in a few lines, to insert the tuple into a corresponding table?

The following doesn't exist, and I know I could make my own function which does this, but this is what I'd like:
PHP Code:
array_to_mysql_tuple($array$table_name); 
Are there any built-in functions that do this or that would make it easier than to build my own function from scratch?

People usually do this going the other way of course, using mysql_query and mysql_fetch_array... I'm trying to do just the opposite. That is, not to go from $query to $result and to $row, but rather $row (a php array) to insertion into a MySQL table.

Thanks in advance,

rm
P.S. - One of the problems in making such a function is distinguishing between strings and values requiring quotes, and other values...

Last edited by rm_-rf_windows; 04-17-2012 at 08:23 AM.
 
Old 04-17-2012, 08:44 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,135

Rep: Reputation: 230Reputation: 230Reputation: 230
Hi

It's not difficult to write such a function. Quotes doesn't matter - they're mandatory for strings and ignored for things like numerical values. It should be something like this:

PHP Code:
function array_to_mysql_tuple($array$table_name)
{
    
$sql "INSERT INTO `$table_name` SET ";
    
$first true;
    foreach (
$array as $key => $value) {
        if (
$first) {
            
$first false;
        } else {
            
$sql .= ",";
        }
        
$sql .= "`$key` = '".mysql_real_escape_string($value)."'";
    }
    return 
mysql_query($sql);

Edit: The mysql_real_escape_string function protects against SQL injection of values, but the keys need to be protected as well. So you need to check for invalid characters in the keys.

Last edited by Guttorm; 04-17-2012 at 08:51 AM.
 
Old 04-17-2012, 09:16 AM   #3
rm_-rf_windows
Member
 
Registered: Jun 2007
Location: Europe
Distribution: Ubuntu
Posts: 205

Original Poster
Rep: Reputation: 23
Too cool Guttorm!!

I had started working on a huge function (well, not huge, but fairly complicated and not very elegant) using "DESCRIBE table" in order to distinguish between strings and numerical values... and then I saw your swift reply.

Many thanks, this thread is resolved and closed!!

rm
 
Old 04-18-2012, 06:25 PM   #4
kelso.b
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Rep: Reputation: Disabled
A slightly more elegant solution:

Code:
function array_to_mysql_tuple($array, $table_name)
{
    array_walk($array, 'mysql_real_escape_string');
    $sql = "INSERT INTO `$table_name` (`%s`) VALUES ('%s')";
    return sprintf($sql, implode('`, `', array_keys($array)), implode("', '", $array));
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PHP - Return Array from a Function issues... GRR! wh33t Programming 7 01-11-2012 04:52 PM
PHP/MySQL | insert on duplicate key run php function?? xsyntax Programming 2 09-10-2010 03:14 AM
Custom mailing function in PHP. Dont want to use built in mail(). linuxlover.chaitanya Linux - Newbie 2 09-26-2009 12:36 AM
PHP: Converting a PHP array into an HTML array koosha Programming 4 08-11-2009 08:47 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM


All times are GMT -5. The time now is 03:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration