PHP cannot assign object parameter values intermittently
For some reason PHP cannot assign values to properties of my object
intermittently! If I keep refreshing the page it works about 4 times out 5
but on the failed one all the values are not assigned I worked out that
the object values are missing.
I have a function to collect a database row and assign it to an object.
The query is not the issue the data is being pulled everytime. The issue
is to do with this line
$obj->$key = str_replace("''","'",$row[$value[0]]);
I do a print out to make sure the value is there and then a print out of
the object parameter directly after and I can see that all the property
values are blank. Not that it is nothing to do with the value itself if I
just set the value as a hard coded string the same thing happens
$obj->$key = 'a'
I don't have this problem with standard variables, the values are always
there it just seems that object parameters values cannot be assigned 1
time out 5.
It doesn't seem to be a code related problem since it works 4 times out 5.
function getRow($class,$where = false,$order = false, $limit = false) {
$obj = new $class();
$sql = "select * from ".$obj->table." where 1 ";
if ($where) {
foreach ($where as $key => $value) {
$opvalue = $this->getOperator($value);
$op = $opvalue[0];
$value = $opvalue[1];
if ($obj->cols[$key][1] == 1)
$sql .= "and ".$obj->cols[$key][0]." $op $value ";
else if ($obj->cols[$key][1] == 2)
$sql .= "and ".$obj->cols[$key][0]." $op '$value' ";
}
}
if ($order)
$sql .= "order by $order ";
if ($limit)
$sql .= "limit $limit ";
$res = $this->runQuery($sql);
//map values
if ($row = mysql_fetch_assoc($res)) {
// print_r($row);
foreach ($obj->cols as $key => $value) {
if ($obj->cols[$key][1] == 3) {
$obj->$key = poststringToArray($row[$value[0]]);
}
else {
$obj->$key = str_replace("''","'",$row[$value[0]]);
// echo "obj->$key = {$row[$value[0]]}<br>";
//
// echo "obj->$key = ".$obj->$key.'<br>';
}
}
// echo '<p>';
// print_r($obj);
// echo '</p>';
}
else {
return false;
}
return $obj;
}
No comments:
Post a Comment