note 20514 added to function.mysql-query

From: Date: Sat, 06 Apr 2002 03:39:06 +0000
Subject: note 20514 added to function.mysql-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-28689@lists.php.net to get a copy of this message
For doing update or insert code, its worth noting that Example 1 seems to be around 60% faster than Example 2 ******************** Example 1 ******************** $rs = mysql_query("SELECT id FROM users WHERE name = 'pete'"); if($row = mysql_fetch_array($rs)) { mysql_query("UPDATE users SET visits=visits+1 WHERE id = '".$row["id"]."'"); } else { mysql_query("INSERT INTO users (name) VALUES ('pete')"); } ******************** ******************** Example 2 ******************** $rs = mysql_query("UPDATE users SET visits=visits+1 WHERE name = 'pete'"); if(mysql_affected_rows() == 0) { mysql_query("INSERT INTO users (name) VALUES ('pete')"); } ******************** Pete Kelly -- http://www.php.net/manual/en/function.mysql-query.php http://master.php.net/manage/user-notes.php?action=edit+20514 http://master.php.net/manage/user-notes.php?action=delete+20514 http://master.php.net/manage/user-notes.php?action=reject+20514

« previous php.notes (#28689) next »