Jump to content

Cleaner mysql insert


tibberous

Recommended Posts

I am trying to make a nice function to do a mysql insert. Right now I have 13 values, which means using a huge mysql query string. I was hoping to make a function that would just take in 13 variables and perform the insert. The variables are all named the same thing as they are in the database.

 

The biggest problem I am having that there doesn't seem to be a good way to get the variables name from the variable, if you are using get_func_args to retrieve it.

Link to comment
Share on other sites

Wrote this just now.....

 

Not the most effecient code ever (the way I create new arrays when I don't need to), but it would work fine.

 

<?php

function AutoInsert($table, $args, $exec = false) {

$fields = array();

$values = array();

foreach($args as $k => $v) {

$fields[] = $k;

$values[] = addslashes($v);

}

unset($args);

$f = implode('\',\'', $fields);

$f = '\'' . implode('\',\'', $values) . '\'';

$q = "INSERT INTO `{$table}` ({$f}) VALUES ('{$v}');";

if($exec) {

return mysql_query($query);

}

else {

return $q;

}

}

?>

[/code]

 

An example:

 

AutoInsert('users', array('uname' => 'Corbin', 'password' => 'somepass', 'email' => 'some@where.com'));

Would return:

INSERT INTO `users` (uname, password, email) VALUES ('Corbin', 'somepass', 'some@where.com');

 

I don't think that's what you're trying to do though....  Lemme see if I can find out how to see the name of a var passed to a function (I don't know if that's possible).

Link to comment
Share on other sites

You could pass a function an array of field names and then an array of values that correspond to the fields.

 

Here is a simple function.

<?php

function insert($fields, $values){
   
   $query = "INSERT INTO table (".implode(',', $fields).") VALUES (".implode(',', $values).")";
   $result = mysql_query($query)or die(mysql_error());

}

?>

 

Example use

<?php

$fields = array("1", "2", "3");
$values = array("uno", "dos", "tres");

insert($fields, $values);

?>

Link to comment
Share on other sites

Another method.

 

<?php

 $array = array(
   'foo' => 'bar',
   'bob' => 'boo',
   'babe' => 'betty'
 )

 function doinsert($arr) {
   $sql = "INSERT INTO tbl SET ";
   foreach($arr as $k => $v) {
     $sql .= "$k = '$v', ";
   }
   $sql = rtrim($sql,',');

   echo $sql; // here you would perform the query.
 }

 doinsert($array);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.