if running function returns server misconfiguration error
function build_path($cid)
{
$result = array();
$DB = new MySQLTable;
$DB->TblName = 'shop_categories';
$where['cat_id']['='] = $DB->CleanQuest($cid);
$res = $DB->Select('cat_id,cat_name,cat_parent', $where);
if($res !== 'false')
{
$pid = mysql_fetch_array($res);
if($pid['cat_parent'] !== 0)
{
Echo $pid['cat_parent'];
build_path($pid['cat_parent']);
} else {
Echo $pid['cat_id'];
return true;
}
}
return false;
}
I can't find an error here. Please help.
Sorry for disturbing you all. The trouble was in comparison 'if($pid['cat_parent'] !== 0)': $pid['cat_parent'] was a string with int(0)
Can i build this function to store full path without using GLOBALS and SESSION vars?
Things to check for:
Is cat_parent ever null?
Are there any recursively parented pairs (A is parent of B, B is parent of A; A->B, B->C, C->A)?
Are there any cases where the row is self parented (A is parent of A)
Try making this modification (for debugging):
function build_path($cid) {
static $done = array();
if (isset($done[$cid])) {
throw new Exception('We have already looked up CID: '.$cid);
} else {
$done[$cid] = true;
}
//.......
}
That will let you tell if you've got a recursive loop going on.
Also, set error_reporting to max, and check your error log. My guess is that it's either a stack overflow (your recursion is going too deep) or a memory limit issue (that it uses up too much memory and is killed)...
Related
I've been out of PHP programming for a while, but I'm almost sure I used to be able to write something like the following:
function checkData($data, $moreData) {
if ($foo != validate($data)) {
return false;
}
if ($bar != validate($moreData)) {
return false;
}
$result = "$foo" . "$bar";
return $result;
}
...where "$foo" and "$bar" haven't been set yet and where the "validate()" function either returns validated data, or returns false if validation fails.
I can't figure out where I'm going wrong, but that code (and variations of it) is throwing an "Undefined variable" error for $myVar.
What would be the correct syntax for this concept?
I think you meant a little bit different thing. Instead of
if ($foo != validate($data)) {
You've been using this
if (!$foo = validate($data)) {
What is happening there is:
1. Call validate function
2. Assign the result to variable
3. Check if condition for that variable.
It's kind of the same as
$foo = validate($data)
if(!$foo)
But as you can understand it's not recommended way of doing the things as it's hard to read and needs explanation(otherwise why do u ask it here, hehe)
PHP has become more strict over the years. You should always instantiate your variables. The following modifications should resolve your "undefined variable" issues:
function checkData($data, $moreData) {
$foo = $bar = null;
if (!$foo = validate($data)) {
return false;
}
if (!$bar = validate($moreData)) {
return false;
}
$result = "$foo" . "$bar";
return $result;
}
It isn't the conditional that is throwing the warning, but where you attempt to assign the non-existent variables to $result. Additionally, your conditionals are checking for comparison rather than assignment.
This is the first time I've ever asked a question on here, but I've used stackoverflow many times in the past to find solutions for problems I'm having in my code.
I'm working on a database transfer page on a php site that uploads csv files and updates the database, and depending on the type of update the user selects this data can be updated/inserted by key. Because of this I want to run DBCC CHECKIDENT after the updates have been made to ensure that new entries will be incremented correctly after the largest key in the table.
This is the php code I'm running:
$getMaxID = new Query("SELECT MAX($tableKeys[$t]) as max from $t", __LINE__, __FILE__);
$maxID = $getMaxID->result(0,'max');
$result = new Query("DBCC CHECKIDENT ('$t', RESEED, $maxID)", __LINE__, __FILE__);
$t is the table name stored in an array of table names.
I get the following error from this code:
There has been a system error. We apologize for the inconvienience.
Error Details: [Microsoft][SQL Server Native Client 11.0][SQL Server]Checking identity information: current identity value '16', current column value '16'.
Line #: 615
File: C:\OCPOS\htdocs\OCPOS\menuTransfer\importMenu.php
Query: DBCC CHECKIDENT ('table', RESEED, 16)
What's confusing me is when I cut an paste DBCC CHECKIDENT ('table', RESEED, 16) into server management studio it works and i get:
Checking identity information: current identity value '16', current column value '16'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
If anyone has any ideas what is causing this or if there's a post I missed that addresses this issue any help would be much appreciated.
Below is the query class. I didn't make it:
class Query{
var $stmt; //hold link to result
var $queryStr; //hold string
var $queryLine;
var $queryFile;
function Query($str, $line, $file)
{
global $conn;
$this->queryStr = $str;
$this->queryLine = $line;
$this->queryFile = $file;
$this->stmt = #sqlsrv_query($conn, $this->queryStr, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET)) or $this->error(sqlsrv_errors());
}
function error($var)
{
echo "
<p style='border: 1px solid #c00; margin: 5px; padding: 5px;'>
There has been a system error. We apologize for the inconvienience.<br/>
Error Details: ". $var[0]['message'] ."<br/>
Line #: $this->queryLine<br/>
File: $this->queryFile<br/>
Query: $this->queryStr<br/>
</p>
";
}
function fetch_array()
{
$array = sqlsrv_fetch_array($this->stmt);
if(is_array($array))
return $array;
else
{
return false;
}
}
function fetch_assoc_array()
{
$array = sqlsrv_fetch_array($this->stmt, SQLSRV_FETCH_ASSOC);
if(is_array($array))
return $array;
else
{
return false;
}
}
function num_rows()
{
return sqlsrv_num_rows($this->stmt);
}
function numrows()
{
return $this->num_rows();
}
function result($row, $var)
{
$array = sqlsrv_fetch_array($this->stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_FIRST);
return $array[$var];
}
function all() {
$return = array();
while ($tmp = $this->fetch_array()) {
$return[] = $tmp;
}
return $return;
}
function arr() {
return $this->fetch_array();
}
function getAll() {
$return = array();
while ($tmp = $this->fetch_array()) {
$return = array_merge($return, $tmp);
}
return $return;
}
function extract($var) {
$rv = array();
while ($tmp = $this->fetch_array()) {
$rv[] = $tmp[$var];
}
return $rv;
}
}
I believe what is happening is that the database driver in PHP is assuming that any output from the database connection (as opposed to Result Sets, which are returned separately) is some form of error. Note that the output you get from SQL Server is actually identical in the two cases, except for some extra context information. I have seen similar problems when the ROW_COUNT setting is left on, with the textual "rows affected/returned" message interpreted as an error.
Since DBCC commands are, to put it nicely, not very standardised, I doubt there is any way to suppress this message at the SQL Server end. However, since you know that it is not really an error, you should be able to simply ignore it on the PHP side. You are already suppressing PHP's own error mechanism with the # operator, so just need a version of your Query method without the or $this->error(sqlsrv_errors());.
Incidentally, this is a good example of the value of separating responsibilities carefully: since the database abstraction class has taken responsibility for formatting the error into HTML and displaying it to the user, you are more-or-less forced to alter or replace that logic. If, instead, it threw an Exception, your calling code could catch this particular case while leaving others to pass through to a global error-handler which formatted the message appropriately.
I'm trying to make an error logging class, I have some functions to set up various output methods like DB, file, return and screen. I want all errors to be stored into an array and when __destruct() is called, I want to stop the client from waiting for data and the log details about errors the user experienced. This way they don't have to report errors to me.
I have 2 modes, a simple GUI to test the functionality and the actual script generates responses in JSON, machine to machine. For GUI the final dump is fine but for JSON it destroys the response. So all error reporting is off and I have to handle any errors that would be dumped on screen myself, hence the $return in function flush_log($return) which make the function return a string if set to true.
After reporting flushing the errors I want to:unset($this->log_arrays)
or empty: $this->log_arrays=Array();, but it is out of scope - I understand why, my function uses a local copy - but how do I reset the array?
[EDIT]:
I tried:
$this->log_arrays = Array();
$this->log_arrays = null;
array popping:
for ($i = 1; count($this->log_arrays); $i++)
{
array_pop($this->log_arrays);
}
But I think none of it can work because within a class function you work with copies of variables, so they're basically out of scope.
[/EDIT]:
This is an already simplified class.. :
<?php
class log_strings extends mysqli
{
private $log_arrays = Array();
public function __construct($output_to_file=false, $output_to_db=true, $fall_back_file=true, $arguments, $ip=null)
{
// Setup mysqli connection, file handle or report error if one or all have failed.
// Also check wich outputs should be used and keep that info for later.
}
public function log($level, $string)
{
$log_arrays[] = Array('level' => $level, 'string' => $string);
}
public function __destruct()
{
$this->flush_log();
}
public function flush_log($return=false)
{
if (!isset($log_arrays) && count($log_arrays) == 0)
{
return true;
}
if ($return)
{
return $this->return_output();
}
else
{
$success = false;
// if enabled, output to db
if ($this->output_to_db)
{
$success = $success || $this->mysqli_output();
}
// if enabled or if db failed and fallback is enabled, output to file
if ($this->output_to_file || ($this->fall_back_file && !$success))
{
$success = $success || $this->file_output();
}
// if neither file or db succeeded, dump on screen
if ($success = false)
{
$this->screen_dump();
}
return true;
}
unset($this->log_arrays); // <= This is what it is all about!
}
private function screen_dump()
{
foreach($this->log_arrays as $array)
{
echo "<strong>{$array['level']}</strong>{$array['string']}<br/>\n";
}
}
private function mysqli_output()
{
// Output to db functionally equal to $this->screen_dump()
}
private function file_output()
{
// Output to file functionally equal to $this->screen_dump()
}
private function return_output()
{
// Return output functionally equal to $this->screen_dump()
}
}
?>
Resetting the array should work
$this->log_arrays = array();
Unset a class property is a very bad idea. Because it may be used in other methods or other classes using a potentionally getter of your class.
I am trying to build an configuration parser for my application I installed APC today, but everytime I try to put an serialized object in the store, it does not get in there and does not. (I am checking with apc.php for my version[3.1.8-dev] on PHP 5.3.16 [My Dev Environment], so I am sure that the data is not in the cache). this is how I pass the data to the cacher:
// The data before the caching
array (
'key' => md5($this->filename),
'value' => serialize($this->cfg)
);
// The caching interface
function($argc){
$key = $argc['key'];
Cache\APC::getInstance()->set($key,$argc['value']);
}
// The caching method described above
public function set($key, $val) {
if (apc_exists($key)) {
apc_delete ($key);
return apc_store($key, $val);
}
else
return false;
}
// the constructor of the configuration class.
// It 1st looks for the configuration in
// the cache if it is not present performs the reading from the file.
public function __construct($filename = '/application/config/application.ini',
$type = self::CONFIG_INI)
{
if (defined('SYSTEM_CACHE') && SYSTEM_CACHE === 'APC'){
$key = md5($filename);
$cfg = APC::getInstance()->get($key);
if (!empty($cfg)) {
print "From Cache";
$this->cfg = unserialize($cfg);
return;
} else {
print "From File";
}
}
}
I did a few tests and there is not a problem with the MD5() key (which I thought while writing this question) nor with APC itself. I am really stuck on this one, nothing odd in the logs, so if anyone can give me at least some directions will be very appreciated.
Thanks in advance!
The problem is was in my code:\
public function set($key, $val) {
/*
*
* If the key exists in the cache delete it and store it again,
* but how could it exist when the else clause is like that...
*/
if (apc_exists($key)) {
apc_delete ($key);
return apc_store($key, $val);
}
// This is very wrong in the current case
// cuz the function will never store and the if will
// never match..
else
return false;
}
NOTE:
Always think and keep your eyes open, if you still can't find anything get off the PC and give yourself a rest. Get back after 10-15 minutes and pown the code. It helps! :D
I have a Yii web service actionQuery that queries a model based on four parameters. There are about 1700 items to be queried total and I'm using a pretty bad web host (iPage). When I run a query with no parameters or extremely common parameters like "a" in string name, I expect to see all or almost all of the rows. Instead I get back a 500 Internal server error that is obviously not being produced by Yii, so it's a pretty bad error. When I try to narrow it down to around 700 or 800 rows, it takes a while but it gets done. How can I correct this error of large data sets producing 500 internal server errors? Is it a max execution time issue? Is there something I need to be doing differently with CDBCriteria?
Here is actionQuery, which is admittedly coded quite poorly.
public function actionQuery()
{
$this->_checkAuth();
switch ($_GET['model'])
{
case 'dogFood':
$criteria = new CDbCriteria();
if ($_GET['name'] && $_GET['name'] !== '0') {
$criteria->addSearchCondition('name_df', $_GET['name']);
}
if ($_GET['ingredients'] && $_GET['ingredients'] !== '0') {
$ingredientsArray = explode(',',$_GET['ingredients']);
foreach ($ingredientsArray as $ingredient) {
$criteria->addSearchCondition('ingredients_df', $ingredient);
}
}
if ($_GET['brand'] && $_GET['brand'] != 0) {
$criteria->addColumnCondition(array('brand_df' => $_GET['brand']));
}
if ($_GET['brandstring'] && $_GET['brandstring'] !== 0) {
$criteriaForBrand = new CDbCriteria();
$criteriaForBrand->addSearchCondition('name_dfb', $_GET['brandstring']);
$brandInQuestion = DogfoodbrandDfb::model()->find($criteriaForBrand);
$brandId = $brandInQuestion->id_dfb;
$criteria->addColumnCondition(array('brand_df' => $brandId));
}
$models = DogfoodDf::model()->findAll($criteria);
break;
default:
$this->_sendResponse(501, sprintf(
'Error: Mode <b>query</b> is not implemented for model <b>%s</b>',
$_GET['model']));
exit;
}
if (empty($models)) {
$this->_sendResponse(200,
sprintf('No items were found for model <b>%s</b>', $_GET['model']));
}
else {
$rows = array();
foreach ($models as $model) {
$rows[] = $model->attributes;
}
$this->_sendResponse(200, CJSON::encode($rows));
}
}
Most likely you are running out of memory. There is a limit set how much memory a PHP program can use: you should see memory exceeded messages in your error.log.
You can try and up the allowed memory for a thread, or for this specific script, but verify in your error log first that this is the issue.