stryker Posted September 28, 2007 Share Posted September 28, 2007 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; } Quote Link to comment Share on other sites More sharing options...
Decepti0n Posted September 28, 2007 Share Posted September 28, 2007 Re: Update MySQL Table Using An Array Could always credit the source... Quote Link to comment Share on other sites More sharing options...
YoungGold Posted September 28, 2007 Share Posted September 28, 2007 Re: Update MySQL Table Using An Array lol deception how do you know where it came from. do you sit on the computer looking all day or something. im not wishing to make fun of you or anything im just wondering how you do it. Quote Link to comment Share on other sites More sharing options...
Isomerizer Posted September 28, 2007 Share Posted September 28, 2007 Re: Update MySQL Table Using An Array lol deception how do you know where it came from. do you sit on the computer looking all day or something. im not wishing to make fun of you or anything im just wondering how you do it. Google parts of the code? Quote Link to comment Share on other sites More sharing options...
YoungGold Posted September 28, 2007 Share Posted September 28, 2007 Re: Update MySQL Table Using An Array ahhhh thanx lol Quote Link to comment Share on other sites More sharing options...
oxidati0n Posted September 28, 2007 Share Posted September 28, 2007 Re: Update MySQL Table Using An Array It doesn't really matter where it comes from, He's just sharing the code. Quote Link to comment Share on other sites More sharing options...
YoungGold Posted September 28, 2007 Share Posted September 28, 2007 Re: Update MySQL Table Using An Array but what he is saying is it would of been nice to post where the source came from so he isnt taking all the credit for it. Quote Link to comment Share on other sites More sharing options...
hamster01 Posted September 28, 2007 Share Posted September 28, 2007 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. |: Quote Link to comment Share on other sites More sharing options...
Decepti0n Posted September 28, 2007 Share Posted September 28, 2007 Re: Update MySQL Table Using An Array I made something similar, but not so many loops and not as many queries :| Quote Link to comment Share on other sites More sharing options...
Zeggy Posted November 11, 2007 Share Posted November 11, 2007 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. Quote Link to comment Share on other sites More sharing options...
hamster01 Posted November 11, 2007 Share Posted November 11, 2007 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'"); } Quote Link to comment Share on other sites More sharing options...
Zeggy Posted November 11, 2007 Share Posted November 11, 2007 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. Quote Link to comment Share on other sites More sharing options...
hamster01 Posted November 11, 2007 Share Posted November 11, 2007 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')); ?> Quote Link to comment Share on other sites More sharing options...
Zeggy Posted November 11, 2007 Share Posted November 11, 2007 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. Quote Link to comment Share on other sites More sharing options...
hamster01 Posted November 11, 2007 Share Posted November 11, 2007 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. Quote Link to comment Share on other sites More sharing options...
Zeggy Posted November 11, 2007 Share Posted November 11, 2007 Re: Update MySQL Table Using An Array 1. Nice, thanks. 2. Care to show me some? 3. Thanks again. Seriously, what is your problem? Did I offend you? Am I wrong in what I said? Do you see subliminal messages from me insulting your mom? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.