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
https://forums.phpfreaks.com/topic/82107-cleaner-mysql-insert/
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' => '[email protected]'));

Would return:

INSERT INTO `users` (uname, password, email) VALUES ('Corbin', 'somepass', '[email protected]');

 

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
https://forums.phpfreaks.com/topic/82107-cleaner-mysql-insert/#findComment-417242
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
https://forums.phpfreaks.com/topic/82107-cleaner-mysql-insert/#findComment-417243
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
https://forums.phpfreaks.com/topic/82107-cleaner-mysql-insert/#findComment-417249
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.