Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-17-2012, 09:18 AM   #1
Registered: Jun 2007
Location: Europe
Distribution: Ubuntu
Posts: 257

Rep: Reputation: 26
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:
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,

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 09:23 AM.
Old 04-17-2012, 09:44 AM   #2
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,293

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

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)."'";

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 09:51 AM.
Old 04-17-2012, 10:16 AM   #3
Registered: Jun 2007
Location: Europe
Distribution: Ubuntu
Posts: 257

Original Poster
Rep: Reputation: 26
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!!

Old 04-18-2012, 07:25 PM   #4
LQ Newbie
Registered: Apr 2012
Posts: 4

Rep: Reputation: Disabled
A slightly more elegant solution:

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));


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 05:52 PM
PHP/MySQL | insert on duplicate key run php function?? xsyntax Programming 2 09-10-2010 04:14 AM
Custom mailing function in PHP. Dont want to use built in mail(). linuxlover.chaitanya Linux - Newbie 2 09-26-2009 01:36 AM
PHP: Converting a PHP array into an HTML array koosha Programming 4 08-11-2009 09:47 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 01:08 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:18 PM.

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