Jump to content
MakeWebGames

Update MySQL Table Using An Array


Recommended Posts

Parameters:

Table: Name of table to update

Data: array of $field->$value with new values

Id Field: Name of field to use as ID field

Id Value: Value of ID field

 

function mysql_update_array($table, $data, $id_field, $id_value) {
foreach ($data as $field=>$value) {
	$fields[] = sprintf("`%s` = '%s'", $field, mysql_real_escape_string($value));
}
$field_list = join(',', $fields);

$query = sprintf("UPDATE `%s` SET %s WHERE `%s` = %s", $table, $field_list, $id_field, intval($id_value));

return $query;
}
Link to comment
Share on other sites

Re: Update MySQL Table Using An Array

This is very poor use of array's, The person who made this is a poor example of a hum being!

Something like this would be better:

<?php
function phrase($array) {
 if (!is_array($array)) return false;
 else {
   foreach($array as $table => $field_array) {
     foreach($field_array as $field => $value) {
       if (is_array($value)) {
         foreach($value as $where => $clause)
           mysql_query("UPDATE $table SET $field=$value WHERE $where=$clause");
          }
       else mysql_query("UPDATE $table SET $field=$value");
     }
   }
 }
}

$array = array('users' => array('money' => array('money+10' => array('userid' => '1'))));
phrase($array);
?>

that's just useless, omg, I just waisted 5 minutes of my time. |:

Link to comment
Share on other sites

  • 1 month later...

Re: Update MySQL Table Using An Array

 

This is very poor use of array's, The person who made this is a poor example of a hum being!

Something like this would be better:

<?php
function phrase($array) {
 if (!is_array($array)) return false;
 else {
   foreach($array as $table => $field_array) {
     foreach($field_array as $field => $value) {
       if (is_array($value)) {
         foreach($value as $where => $clause)
           mysql_query("UPDATE $table SET $field=$value WHERE $where=$clause");
          }
       else mysql_query("UPDATE $table SET $field=$value");
     }
   }
 }
}

$array = array('users' => array('money' => array('money+10' => array('userid' => '1'))));
phrase($array);
?>

that's just useless, omg, I just waisted 5 minutes of my time. |:

And that is a very inefficient use of loops and database queries :)

If you had ten elements in an array, there'd be 10 queries on that page. It would be better to construct the query in the loop, then run the query after it's done.

Link to comment
Share on other sites

Re: Update MySQL Table Using An Array

 

This is very poor use of array's, The person who made this is a poor example of a hum being!

Something like this would be better:

<?php
function phrase($array) {
 if (!is_array($array)) return false;
 else {
   foreach($array as $table => $field_array) {
     foreach($field_array as $field => $value) {
       if (is_array($value)) {
         foreach($value as $where => $clause)
           mysql_query("UPDATE $table SET $field=$value WHERE $where=$clause");
          }
       else mysql_query("UPDATE $table SET $field=$value");
     }
   }
 }
}

$array = array('users' => array('money' => array('money+10' => array('userid' => '1'))));
phrase($array);
?>

that's just useless, omg, I just waisted 5 minutes of my time. |:

And that is a very inefficient use of loops and database queries :)

If you had ten elements in an array, there'd be 10 queries on that page. It would be better to construct the query in the loop, then run the query after it's done.

You should really try not to confuse yourself while typing..

That was just an example with arrays..

function phrase($user, $field, $value) {
mysql_query("UPDATE users SET $field = '$value' WHERE userid = '$user'");
}
Link to comment
Share on other sites

Re: Update MySQL Table Using An Array

What do you mean, confuse myself? I'm not confused :)

What I was saying was that your function using arrays isn't efficient at all.

The original function that stryker posted was much better than the function you posted.

Link to comment
Share on other sites

Re: Update MySQL Table Using An Array

 

What do you mean, confuse myself? I'm not confused :)

What I was saying was that your function using arrays isn't efficient at all.

The original function that stryker posted was much better than the function you posted.

And there you go again, it may be a mental disorder, you should go checked. (;

<?php
function phrase($array) {
mysql_query("UPDATE `" . $array[0] . "` SET `" . $array[1] . "` = '" . $array[2] . "' WHERE `" . $array[3] . "` = '" . $array[4]);
}
phrase(array('users','name','n00b','name','Zeggy'));
?>
Link to comment
Share on other sites

Re: Update MySQL Table Using An Array

Haha, okay, but it's still as useless as before, as it can't update multiple columns in the same query.

The original function could update multiple columns in the same query. That's what I meant by constructing the query.

Link to comment
Share on other sites

Re: Update MySQL Table Using An Array

 

Haha, okay, but it's still as useless as before, as it can't update multiple columns in the same query.

The original function could update multiple columns in the same query. That's what I meant by constructing the query.

1) Go skullf*ck yourself.

2) Go take a look at my examples of work.

3) Get a life, or rent one in your case, because nobody would be stupid enough to sell you one.

Link to comment
Share on other sites

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.   Paste as plain text instead

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