Re: Empty Array?

From: Date: Fri, 05 Oct 2007 23:59:20 +0000
Subject: Re: Empty Array?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-262855@lists.php.net to get a copy of this message
At 7:43 AM -0400 10/5/07, Dan Shirah wrote:
Ah, what a lovely case of the Friday morning brain farts! I have a query that selects some data from a table based on the current ID selected. If the query does not return any results, I want it to continue to another query that will insert a record into the table.
Dan: I wouldn't check for an empty array, but rather if affected rows > 0 -- like so: // check to see if there is a record for this page $query = "SELECT * FROM pages WHERE page_id = $page_id"; $result = mysql_query($query) or die('Error, query 1 failed ' . mysql_error(). $query); $seg = mysql_real_escape_string($segment); if (mysql_affected_rows() > 0) // if record exist then add data to it { $query = "UPDATE pages SET segment_id= '$seg' WHERE page_id= '$page_id' "; $result = mysql_query($query) or die('Error, query 2 failed ' . $query); } else // else create new record and then add data to it { $query = "INSERT INTO pages ($page_id, segment_id) VALUES ( '$page_id' , '$seg' )"; $result = mysql_query($query) or die('Error, query 3 failed ' . mysql_error()); } Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com

« previous php.general (#262855) next »