Jump to content

non-logged HTTP 500 error


Greywacke
 Share

Recommended Posts

hi again,i've traced the HTTP 500 error on this page, to the getsiblings function in the ajax_config.php xml generator.

// ... some irrelevant lines of codefunction getsiblings($children,$j = 0,$level = 0) {	echo "	<binds level=\"1\">\n";	for ($i = $j; $i < count($children); $i++) {		$sql = "SELECT * FROM 1_regions WHERE bigint_ParentRegionID = (SELECT bigint_ParentRegionID FROM 1_regions WHERE bigint_RegionID = ".$children[$i].") ORDER BY text_RegionDescription ASC;";		$result = mysql_query_errors($sql , $conn , __FILE__ , __LINE__ , false);		if ($result) {			while ($row = mysql_fetch_array($result)) {				echo "		<bind name=\"".xmlentities($row["text_RegionDescription"])."\" value=\"".$row["bigint_RegionID"]."\" level=\"".				$level."\" ".((($children[$i] == $row["bigint_RegionID"]) && ($i == count($children) - 1))?"selected=\"selected\" ":"")."/>\n";				if ($children[$i] == $row["bigint_RegionID"]) getsiblings($children,$i,$level + 1);			}		}	}	echo "	</binds>\n";}function getparents($child,$array = array()) {	if ($child == 0)  {		//print_r(array_reverse($array));		getsiblings(array_reverse($array),0,0);	} else {		array_push($array,$child);		$sql = "SELECT bigint_ParentRegionID FROM 1_regions WHERE bigint_RegionID = ".$child.";";		$result = mysql_query_errors($sql , $conn , __FILE__ , __LINE__ , false);		if ($result) {			if ($row = mysql_fetch_array($result)) {				getparents($row["bigint_ParentRegionID"],$array);			}		}	}}// ... some more irrelevant lines of codeswitch ($q) {// ... some more irrelevant lines of code	case 29: // return cascaded regions		$rid = $_GET["r"];		//echo $rid;		getparents($rid);		break;// some more irrelevant lines of code

the getsiblings function is passed the following ordered array of region id's, retrieved by a previous function to collect the selected parent id's.

Array(	[0] => 100	[1] => 2	[2] => 102020	[3] => 99501)

it would obviously start stepping through this array at element 0, which is the id for United States of America (a country). the next one, id 2 is the id for the state of Alaska. the next is the county, Anchorage - and the last is the zipcode 99501 in the city of Anchorage. testing these mysql queries - it successfully executes in phpmyadmin. the purpose of this code, is to do a reverse lookup of the regions, building a list to select a region programmatically, that is bound on various parts of the site to another group of information, for example supplier regions. can anyone perhaps see what is triggering this HTTP 500 error? i cannot seem to locate a valid error log via ftp... :)... noted a discrepancy in passing $level variables to the xml output - as been corrected in this post as well as the code. still getting the HTTP 500 error :/

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

Are you using your own error log or checking for a server one? You can set it up to use your own, as long as PHP has write permissions for the file. I use this code to send all errors to a file called error.log in the same directory as the script with these settings:

ini_set('log_errors', 1);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('display_errors', 0);error_reporting(E_ALL);

Link to comment
Share on other sites

thanks :) might be just the edge my php apps need :)

Link to comment
Share on other sites

hmmm strange though... 0o the error in the log is:

[08-Feb-2011 12:30:57] PHP Notice: Undefined variable: conn in /home/dwtphovu/public_html/intellisource.co.za/prod_5683194816/scripts/ajax_config.php on line 308
however, getparents is called and $conn exists, but within the iteration of the second function getsiblings that variable is not accessable! 0o :)what would have to change in order to make it so?here is the typical mysql_query_errors function call used from within countless functions:
	$sql = "SHOW COLUMNS FROM 19_consumers;";	$result = mysql_query_errors($sql , $conn , __FILE__ , __LINE__ , false);

it works from within countless other functions... how does the scope of this $conn variable change? not too clear on that i am afraid - and how to remedy in this situation?

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

i've repaired these discrepancies that caused the http 500 errors (by referring to $conn from the $GLOBALS namespace), but now - even after clearing cache and refreshing, it still gets the error!sadly though, even with the custom error logging code - it does not log the errors! :)

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

it's almost as if the php generated xml documents are being cached somewhere on a proxy! i am at wits end again :/it's ignoring the custom php errors and location as if it was not written at all! it still displays the http 500 error , despite the document requested being repaired... i refer to the previous post with more urgency.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

It may be that the custom error code is not being executed for whatever reason (make sure it's at the top of a global include file or something similar, and outside of any control structures), or it may be that PHP is running into a startup error, like max time spent processing the request data, so that it doesn't apply the run-time settings.

Link to comment
Share on other sites

ok, the http 500 error is gone now - but the functions don't return the array, despite passing it as parameter to itsself.i've realised this is due to the function not being written to do what i need it to do. the function is currently as follows, followed by how they are called, and finally the traversed database resultset format:

function getparents($child,$array = array()) {	if ($child == 0)  {		return array_reverse($array);	} else {		array_push($array,$child);		$sql = "SELECT bigint_ParentRegionID FROM 1_regions WHERE bigint_RegionID = ".$child.";";		$result = mysql_query_errors($sql , $conn , __FILE__ , __LINE__ , true);		if ($result) {			if ($row = mysql_fetch_array($result)) {				getparents($row["bigint_ParentRegionID"],$array);			}			mysql_free_result($result);		}	}}$r = $_GET["r"];$rz = getparents($r);print_r($rz);getsiblings($rz);[b]bigint_RegionID		text_RegionDescription		bigint_ParentRegionID[/b]100				United States of America		02				Alaska					100102020				Anchorage				299501				Anchorage				102020

unfortunately i am not seeing how the getparents function needs to be modified in order to return an array of region id's, eg $rz = Array(99501, 102020, 2, 100); - stepping through the ancestors recursively till it reaches a top level ancestor (with parent id of 0)... :)this array needs to be passed to the getsiblings function in order to write the xml list of ancestors and their siblings.clearly - recursive functions are not my strong point yet. :)

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

yes, what i do not see is how to return as one array... even if returning the single values with each step. hang on - if i return the id in each step - and add the calling function to the array with each step.... thing is this is not a multitier list it is single tier, with about 4 levels to be returned as an array.

Link to comment
Share on other sites

I don't think you understand how recursive functions like this work. You use this code to decide whether to call the function again:

			if ($row = mysql_fetch_array($result)) {				getparents($row["bigint_ParentRegionID"],$array);			}

And then you use this code to check the value:

	if ($child == 0)  {		return array_reverse($array);	}

So, it the value is 0 then you return the array. But you're not doing anything with the return value. You need to get the return value:

				$retval = getparents($row["bigint_ParentRegionID"],$array);

After that, you can do whatever you want with that, like use array_merge to merge the return array with $array and combine the values into the same array. At the end of that else block, it needs to return the final array. The function always needs to return a value, you can't have one situation like the else block not return anything or else it won't work right.

Link to comment
Share on other sites

yeah try to do that as follows - and it returns an http 500 internal server error again... :/

function getparents($child,$array = array()) {	if ($child == 0) {		return array_reverse($array);	} else {		array_push($array,$child);		$sql = "SELECT bigint_ParentRegionID FROM 1_regions WHERE bigint_RegionID = ".$child.";";		$result = mysql_query_errors($sql , $conn , __FILE__ , __LINE__ , true);		if ($result) {			if ($row = mysql_fetch_array($result)) {				$return = getparents($row["bigint_ParentRegionID"],$array);			}			mysql_free_result($result);		}	}	return $return;}

Link to comment
Share on other sites

wonderful! and it works!!! :) but only problem now is the getsiblings recursive function which is throwing an error :/

[09-Feb-2011 10:43:27] PHP Notice: Undefined variable: conn in /home/dwtphovu/public_html/intellisource.co.za/prod_5683194816/scripts/ajax_config.php on line 307
this is what getsiblings looks like:
function getsiblings($children,$j = 0,$level = 0) {	echo "	<binds level=\"1\">\n";	for ($i = $j; $i < count($children); $i++) {		$sql = "SELECT * FROM 1_regions WHERE bigint_ParentRegionID = (SELECT bigint_ParentRegionID FROM 1_regions WHERE bigint_RegionID = ".$children[$i].") ORDER BY text_RegionDescription ASC;";		$result = mysql_query_errors($sql , $conn , __FILE__ , __LINE__ , true);		if ($result) {			while ($row = mysql_fetch_array($result)) {				echo "		<bind name=\"".xmlentities($row["text_RegionDescription"])."\" value=\"".$row["bigint_RegionID"]."\" level=\"".				$level."\" ".((($children[$i] == $row["bigint_RegionID"]) && ($i == count($children) - 1))?"selected=\"selected\" ":"")."/>\n";				if ($children[$i] == $row["bigint_RegionID"]) getsiblings($children,$i,$level + 1);			}			mysql_free_result($result);		}	}	echo "	</binds>\n";}

weird in that it doesn't send a blank/undefined $conn through - it does however, accept it and use the default connection if not valid - the way mysql_query_errors was coded. for some reason it is processing getparents without any incident!also - surely a recursive function does not have to return a value?

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

well, i do more or less this within the mysql_query_errors function, but why can i not pass an empty/undefined variable to this function here, when it has been done 10,000 times in other functions - especially noted getparents, which works as expected now, without defining $conn as the global variable, outside of the function? the error is obviously before i am doing the mysql query in the function.and this is what the mysql_query_errors function looks like:

function mysql_query_errors($sql, $conn, $doc, $line, $expret = false) {	$line--;	$err = "";	[b]if (!$conn) $conn = $GLOBALS["conn"];[/b]	$result = mysql_query($sql,$conn);	if (mysql_errno() > 0) {		$err = "ERROR #".mysql_errno().": ".mysql_error()." in ".$doc." on Line ".$line;	} elseif ($expret) {		if (is_null($result)) {			$err = "NOTICE #0504: A null value was returned by the query defined in ".$doc." on line ".$line--;		} elseif ($result === false) {			$err = "NOTICE #0502: A false value was returned by the query defined in ".$doc." on line ".$line--;		} elseif ($result === true) {			$err = "NOTICE #0501: A true value was returned by the query defined in ".$doc." on line ".$line--;		} elseif (mysql_num_rows($result) < 1) {			$err = "NOTICE #0500: An empty result set was returned by the query defined in ".$doc." on line ".$line--;		}		array_push($GLOBALS["sql"],$sql."\n",$err."\n");	}	return $result;}

in short - why would this have to be specified here and not in the other functions?

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

okay, still on the getsiblings function - currently as follows:

function getsiblings($children,$j = 0,$level = 0) {	echo "	<binds level=\"1\">\n";	for ($i = $j; $i < count($children); $i++) {		global $conn;		$sql = "SELECT * FROM 1_regions WHERE bigint_ParentRegionID = (SELECT bigint_ParentRegionID FROM 1_regions WHERE bigint_RegionID = ".$children[$i].") ORDER BY text_RegionDescription ASC;";		$result = mysql_query_errors($sql , $conn , __FILE__ , __LINE__ , false);		if ($result) {			while ($row = mysql_fetch_array($result)) {				$selected = "";				if ($children[$i] == $row["bigint_RegionID"] && $i == count($children) - 1) $selected = "selected=\"selected\" ";				echo "		<bind name=\"".xmlentities($row["text_RegionDescription"])."\" value=\"".$row["bigint_RegionID"]."\" level=\"".$level."\" ".$selected."/>\n";				if ($children[$i] == $row["bigint_RegionID"]) getsiblings($children,$i,($level+1));			}			mysql_free_result($result);		}	}	echo "	</binds>\n";}

this function still generates an internal server error (http 500), without generating error logs despite those lines of code in the global include! :)i'm really at wits end here *sighs* :/

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

You can pass it an undefined variable if you want and check that later, it's not going to cause a fatal error if you do that, only a notice.

the error is obviously before i am doing the mysql query in the function.
How do you know that?
this function still generates an internal server error (http 500), without generating error logs despite those lines of code in the global include!
Create a test file which first includes the global include to set the error settings, and then have it include your other file. If there's a syntax error in that file you'll want to set the error settings first, then include the problem file.
Link to comment
Share on other sites

i was referring to the function getparents, which works 100% as expected. the problem is with the second function, getsiblings because it processes fine if i comment that function's call, thus there seems to be something wrong with the getsiblings function. the reason i say there is nothing wrong with the call of mysql_query_errors, is because it is called exactly as it is in the function that is working now (getparents).i will attempt this towards finding a solution for the problem, thanks for the advise - i REALLY hope it helps me find the problem!

Link to comment
Share on other sites

afraid i do not see an issue with this function - it's basically just to convert some invalid xml characters to numeric character references, due to files reverting to ascii from utf-8 from time to time on the server - htmlentities could not be used (not valid entities for xml).

function xmlentities($value) {	// replace characters	if (is_array($value)) {		foreach ($value as $key => $val) {			$value[$key] = xmlentities($val);		}	} else {		//replace $value's invalid xml characters with entities and unicode characters		// used with Hex NCR's (Hexadecimal Numeric Character Representations).		$patterns = array(				'&',			// ampersand symbol				'<',			// less than symbol				'>',			// greater than symbol				'"',			// double quotation mark				'©',			// © copyright symbols				'ë',			// e with diaresis				'è',			// e with grave				'é',			// e with acute				'"',			// left slanting double quotation mark				'"'			// right slanting double quotation mark			);		$replacements = array(				'&',				'<',				'>',				'"',				'©',				'ë',				'è',				'é',				'“',				'”'			);		$value = utf8_encode($value);	// convert string to unicode to replace characters		$value = str_replace($patterns, $replacements, $value);	}	return $value;}

is there perhaps some "rule" against calling a recursive function from within another recursive function?

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

ok have created a test.php as follows, here:

<?phpinclude("/home/dwtphovu/public_html/intellisource.co.za/prod_5683194816/includes/content/dwtphovu_f3rr37y.php");function getsiblings($children,$j = 0,$level = 0) {	echo "	<binds level=\"1\">\n";	for ($i = $j; $i < count($children); $i++) {		global $conn;		$sql = "SELECT * FROM 1_regions WHERE bigint_ParentRegionID = (SELECT bigint_ParentRegionID FROM 1_regions WHERE bigint_RegionID = ".$children[$i].") ORDER BY text_RegionDescription ASC;";		$result = mysql_query_errors($sql , $conn , __FILE__ , __LINE__ , false);		if ($result) {			while ($row = mysql_fetch_array($result)) {				$selected = "";				if ($children[$i] == $row["bigint_RegionID"] && $i == count($children) - 1) $selected = "selected=\"selected\" ";				echo "		<bind name=\"".xmlentities($row["text_RegionDescription"])."\" value=\"".$row["bigint_RegionID"]."\" level=\"".$level."\" ".$selected."/>\n";				if ($children[$i] == $row["bigint_RegionID"]) getsiblings($children,$i,$level+1);			}			mysql_free_result($result);		}	}	echo "	</binds>\n";}header("Content-type: text/plain; charset=utf-8");$rz = array(100,2,102020,99501);getsiblings($rz);?>

just the function and the array, but i still get the following:

Internal Server ErrorThe server encountered an internal error or misconfiguration and was unable to complete your request.Please contact the server administrator, webmaster@intellisource.performatix.net and inform them of the time the error occurred, and anything you might have done that may have caused the error.More information about this error may be available in the server error log.Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
without any errors being logged! the functions called exist in the include, and there is nothing wrong with them - they work perfectly in other places they are called... :/
Link to comment
Share on other sites

ok... the problem generating the http 500 error was somehow linked to calling the subfunction passing $i into it and not $i + 1.the problem now is it is printing out the siblings down the array for each getsiblings call. how could i make it only print out the relevant section of the tree?the page can be viewed at http://www.intellisource.co.za/test.php, and does not get a http 500 error any more.

Link to comment
Share on other sites

oh this was relatively easy... :) removed the for iteration, but kept $i and $j being passed. :)

function getsiblings($children,$j = 0,$level = 0) {	$i = $j;	if ($j == 0) echo "	<binds level=\"1\">\n";	$sql = "SELECT * FROM 1_regions WHERE bigint_ParentRegionID = (SELECT bigint_ParentRegionID FROM 1_regions WHERE bigint_RegionID = ".$children[$i].") ORDER BY text_RegionDescription ASC;";	$result = mysql_query_errors($sql , $conn , __FILE__ , __LINE__ , false);	if ($result) {		while ($row = mysql_fetch_array($result)) {			$selected = "";			if ($children[$i] == $row["bigint_RegionID"] && $i == count($children) - 1) $selected = "selected=\"selected\" ";			echo "		<bind name=\"".xmlentities($row["text_RegionDescription"])."\" value=\"".$row["bigint_RegionID"]."\" level=\"".$level."\" ".$selected."/>\n";			if ($children[$i] == $row["bigint_RegionID"]) getsiblings($children,$i+1,$level+1);		}		mysql_free_result($result);	}	if ($j == 0) echo "	</binds>\n";}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...