Re: Empty Array?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux