I have some code that runs fine until it hits this:
if(array_key_exists($snIdx, $fields));
$surnameField = trim($fields[$snIdx]);
or other version I tried:
if(isset($fields[$snIdx));
$surnameField = trim($fields[$snIdx]);
The $snIdx = -1.
It gives me Undefined offset error at second line ($surname = trim...).
I think I don't need to paste rest of code as the exception says there is sth wrong with those functions. My PHP version is 5.4.16.
Remove the semicolon from the end of the if line. Otherwise it's equivalent to:
if( something) {
// no-op
}
$surnameField = trim($fields[$snIdx]); // undefined offset error.
Related
I have a bit of PHP code that works fine on my production server but not on my test server. Here is the code:
function callProcedure0(&$connection, $procname, $dofunction)
{
mysqli_multi_query($connection, "CALL " . $procname . "();");
$first_result = 1;
do
{
$query_result = mysqli_store_result($connection);
if ($first_result)
{
$dofunction($query_result);
$first_result = 0;
}
if ($query_result)
{
mysqli_free_result($query_result);
}
$query_result = NULL;
} while(mysqli_next_result($connection));
}
...
function doGenres($in_result)
{
global $genre_array, $game_array, $genre_order_array;
$genre_count = 1;
// foreach is necessary when retrieving values since gaps may appear in the primary key
while ($genre_row = mysqli_fetch_row($in_result)) // line 81 is here!
{
$genre_array[] = $genre_row[0];
$genre_order_array[$genre_row[1] - 1] = $genre_count;
$game_array[] = [[],[]];
$genre_count += 1;
}
}
...
callProcedure0($con, "get_genres_front", doGenres); // line 138 is here!
The "get_genres_front" bit refers to a stored procedure on my database. Here are the errors:
Notice: Use of undefined constant doGenres - assumed 'doGenres' in /opt/lampp/htdocs/keyboard/keyboard.php on line 138
Again, there are no problems on the production server which is using Apache 2.2.23, MySQL 5.1.73-cll, PHP 5.4.26. The test server where things are broken is running Apache 2.4.10, MySQL 5.6.21, PHP 5.5.19.
Did something change in recent software versions? Thanks.
[edit]
This is not a duplicate question. I'm worried about the first error. I already know what to do about the second error which I have deleted.
The code you have posted is wrong, you must pass function name as string and then use call_user_func to invoke this function.
In your callProcedure0 function change
$dofunction($query_result);
to
call_user_func($dofunction, $query_result);
And then call it with the function name as string like this
callProcedure0($con, "get_genres_front", "doGenres");
The above code could work also with invoking the function with
$dofunction($query_result);
on some php versions, but the line where you pass the function name it should be string, otherwise PHP assumes it is a constant.
This works on my test environment, but on my live server there is a later version of PHP which is throwing up an error and breaking my program
The code is
$oldFile = fopen("D:/ftpfolderreport/report/" . $last_file, "r");
while(!feof($oldFile))
{
$buffler = fgets($oldFile);
$bufflerArray = explode(",", $buffler);
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}
fclose($oldFile);
This line:
$oldFileArray[$key] = $bufflerArray[1];
Is throwing out this error
Notice: Undefined offset: 1 in D:\apps\wamp\www\Compliance2\compareFtpReports.php on line 57
I think this is to do with how I'm adding the $key variable inside the argument. I've tried it as ["$key"] and ['$key'] but it doesn't like it.
I have tried defining the key variable earlier in the program but still doesn't like it. I've been searching around online but can't find anything of help. Anyone any ideas?
Thanks,
Stephen.
add checks for empty
if (!empty($bufflerArray[1])) {
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}
I'm trying to reset the pointer to the first record in CodeIgniter. Consider the following greatly simplified code:
$query_milestones = "SELECT * FROM milestones";
$milestones = $this->db->query($query_milestones);
foreach($milestones->result() as $milestoneRow){
// do something
}
$milestones->data_seek(0); // <--- This gives me Fatal error.
foreach($milestones->result() as $milestoneRow){
// do something else
}
This gives me:
Fatal error: Call to undefined method CI_DB_mysql_result::data_seek()
How can I do a mysql_data_seek with CodeIgniter?
UPDATE: It seems VERY odd to me but resetting the pointer apparently is not necessary. The following does what I want but not what I expect:
$query_milestones = "SELECT * FROM milestones";
$milestones = $this->db->query($query_milestones);
foreach($milestones->result() as $milestoneRow){
// do something
}
// As soon as the foreach is reached, the first record is retrieved again.
foreach($milestones->result() as $milestoneRow){
// do something else
}
try #$this->db->data_seek(0); instead of $milestones->data_seek(0);
You can try $milestones->_data_seek(0); but I have not used CodeIgniter in a while and supposedly it is a private method, at least it used to be.
Notice (8): Use of undefined constant inList - assumed 'inList' [CORE\Cake\Utility\ClassRegistry.php, line 168]
This notice has been bugging me for a while know, and I do not know how to fix it.. It was not really affecting my project earlier since its just a notice msg, but now, it is not letting me show an error message which I am trying to display to the user.
Iv got this function
public function validate_form(){
if($this->RequestHandler->isAjax()){
$this->request->data['Donor'][$this->params['form']['field']] = $this->params['form']['value'];
$this->Donor->set($this->data);
if($this->Donor->validates()){
$this->autoRender = FALSE;
}else{
$error = $this->Donor->validationErrors;
$this->set('error',$error[$this->params['form']['field']]);
}
}
}
The above is the action to which my post request submits to. Then it executes the following to display the error
if (error.length > 0) {
if ($('#name-notEmpty').length == 0) {
$('#DonorName').after('<div id="name-notEmpty" class="error-message">' + error + '</div>');
}
}else{
$('#name-notEmpty').remove();
}
The problem is that instead of the relevant error in my newly created div... I get that notice 8 from cake! Please if anyone knows why this is happening, I appreciate your aid on this one..
TLDR:
Do a project-wide find for 'inList' and find the spot where it either doesn't have quotes around it, or, if it's supposed to be a variable, is missing it's $.
Explanation:
You get that error when you try to use a PHP Constant that doesn't exist. Usually you're not actually TRYING to use a constant, but instead just forgot to wrap quotes around something or forgot to add the $ before a variable.
Examples:
$name = "Dave";
echo name; // <-- WOAH THERE, there is no Constant called name (missing $)
$people = array('Dave' => 'loves pizza');
echo $people[Dave]; // <-- WOAH THERE, no Constant called Dave (missing quotes)
Most likely somewhere else in your code you are using 'inList' as an array key but you don't have it quoted.
Example: $value = $myArray[inList];
It still works without quoting inList but it causes the notice message you're seeing.
i have some code like this:
function getAuthorizedPFComponents($pfState)
{
$authorizedPFComponents = new \stdClass();
$compTypeMap=array('platform'=>'pfAuthorizations','mainSite'=>'mainSiteAuthorizations','microSites'=>'microSiteAuthorizations','apps'=>'appAuthorizations');
foreach($compTypeMap as $compType=>$tagName)
{
$authorizationsNode=$this->pfAuthXMLDOM->getElementsByTagName($tagName)->item(0);
foreach($authorizationsNode->getElementsByTagName('authorizations') as $pfComponentAuthElem)
{
foreach($pfComponentAuthElem->getElementsByTagName('allow') as $allow)
{
switch($allow->getAttribute('orgCode'))
{
case 'K_ALL':
{
$authorizedPFComponents->$compType->{$pfComponentAuthElem->getAttribute('pfComponentCode')}->storeCode=$allow->getAttribute('storeCode');
}
}
It shows a warning:
Warning: Creating default object from empty value
The warning is traced back to the code under case K_ALL:
This was too long for a mere comment, so I'll remove it when it has outlived its usefulness.
First thing you should do is make the code simpler; there's a lot of stuff going on in that one statement:
$compCode = $pfComponentAuthElem->getAttribute('pfComponentCode');
$storeCode = $allow->getAttribute('storeCode');
And add debug code:
var_dump($authorizedPFComponents->$compType);
var_dump($authorizedPFComponents->$compType->$compCode);
$authorizedPFComponents->$compType->$compCode->storeCode = $storeCode;
Consider this code:
$x->y = 'test';
If $x is not defined, it will issue a warning:
Warning: Creating default object from empty value in xxx
The same goes for your chain of references:
$authorizedPFComponents->$compType->$compCode->storeCode
If any of those paths is empty, the next ->yyy will cause that warning.