Undefined Index Laravel 4.0 - Possible Bug? - php

I feel like I'm taking crazy pills here, so I hope I'm simply missing the obvious. Please find below exhibit a, my code including var_dumping:
$statuscheck = $read->readResponse();
$statusResponse = $statuscheck['StatusCheckResponse'];
echo "<pre>"; var_dump( $statuscheck, $statusResponse, $statuscheck['StatusCheckResponse']); die;
Which gives us the following results : Here be the results
That seems all normal right? Notice I've passed the $statuscheck['StatusCheckResponse'] into the $statusResponse var and var_dumped them both. Both giving identical results in the above output.
But watch what happens when I take the var_dump away.
This is what happens a giant undefined index error! How is this possible? What am I doing wrong?

If that code is inside a loop, or executed more than once on a single request, it could be a later iteration doesn't have the required indexes set.
Try replacing the die with this:
if(empty($statuscheck['StatusCheckResponse'])) die;

Related

Trying to get property 'totalInterestPaid' of non-object when object is present

I have never come across a issue quite like this. Laravel is saying that I am trying to get a property of a non-object when the variable I am referring to is indeed a object.
$balanceToPush = DB::table('loan_balances')
->where('loansID', $loan->id )
->first();
$balanceToPush->totalPaid = number_format($balanceToPush->totalInterestPaid + $balanceToPush->totalTaxPaid + $balanceToPush->totalPrincipalPaid, 2, '.', ',');
//dd($balanceToPush);
array_push($loanBalancesArray, $balanceToPush);
The above code fails at the first variable in the number formatter $balanceToPush. I can get rid of the first two and just have, for example, $balanceToPush->totalTaxPaid and it will throw the same error.
However, when I dump the $balanceToPush variable it definitely is a object with the fields I am trying to access:
{#1249 ▼
+"id": 2
+"loansID": 2
+"loansBalance": 9000.0
+"totalInterestPaid": 1670.0
+"totalPrincipalPaid": 1000.0
+"totalTaxPaid": 551.1
+"created_at": "2021-09-16 10:12:27"
+"updated_at": "2021-09-16 10:12:27"
+"totalPaid": "3,221.10"
}
What I have tried?
At this point, I copy and pasted the exact variables from the dd($balanceToPush) and pasted them into the code. Same issue.
I tried to clear the cash by doing php artisan clear:cache, still the same thing.
The most confusing part of this all, was that it was all working fine throughout the day. I then took a break for a few hours came back and then this error popped up.
All help is appreciated.
This is not really Laravel that is complaining, this is PHP itself.
Althrough it can be confusing, it is very very very unlikely PHP is making a mistake, and you can assume that at some point, you are indeed trying to get a property from a non object.
One of the common mistakes is to use dd in a loop (don't know if that's the case here, but if it is, then you only "test" the first iteration then the script stops).
Try this, I rearranged your code so it is easier to debug:
dump($loan); //the same output as dd, but without stopping the script
$balanceToPush = DB::table('loan_balances')
->where('loansID', $loan->id )
->first();
dump($balanceToPush);
//$balanceToPush->totalPaid = number_format($balanceToPush->totalInterestPaid + $balanceToPush->totalTaxPaid + $balanceToPush->totalPrincipalPaid, 2, '.', ',');
array_push($loanBalancesArray, $balanceToPush);
You should get a null output at some point. If it works, try again but uncomment the number_format part.
If you are still getting the same error without any null values in the dumps, can you edit your question with the entire exception trace and a wider portion of your code please?

php serialize undefined variable

I have the following;
$foo;
$foo = serialize($foo);
print_r($foo);
it gave me a result of N;
Now I do not know if this is unique to the system i am using or if this is a normal result. All I know is it was causing major problems with my site.
My question is... is this normal when serializing an undefined variable?If so can someone please explain why it outputs this result.Thank you for your time.
It appears that the string N; is the serialize()d form of null. (See this codepad)
However, two things to note:
You should be defining all variables before using them, even if you just declare them as null. Otherwise you'll get a notice about $foo being an undefined variable. Some argue that you can hide warnings, but this is poor practice. You should prevent them completely by using sensible defaults for all variables.
Typically, one does not need to read the output of serialize(). You can store it on a DB, in a file, or in a memory cache system like Memcached, APC, or Redis. However, you will never need to understand what the output of serialize() means thanks to unserialize().
It works as expected:
<?php
$foo;
$foo = serialize($foo);
var_dump($foo); // Print the string "N"
$unserialized = unserialize($foo);
var_dump($unserialized); // Print NULL
But why are you looking at the output of serialize? This should not be relevant to your website. What matters is that unserialize gives you back the correct data.

Using mysql_fetch_assoc twice in a single PHP script

I usually try to minimize calls to MySQL where possible, but I've finally encountered the case where I have to do multiple calls to MySQL in a single script.
It looks like you can't use mysql_fetch_assoc twice in a single script(!).
It seems that mysql_data_seek is the solution, but I cannot seem to get it to work.
To be clear, I have two queries I need to make. The second query depends on results from the first... Here's the structure:
$result = mysql_query($query1);
while($row = mysql_fetch_assoc($result)){
$pos = $row['position'];
}
mysql_free_result($result); // result freed per comment below.
$query2 = ' '; //... dependent on $pos - in mysql shell this returns results!
$result2 = mysql_query($query2)
while($row = mysql_fetch_assoc($result2)){
echo $row['id'];
}
What happens above is that the second while loop returns no results even though the query should have nontrivial rows.
Just to be sure:
Is this how you clear the pointer from the previous result to be able to use mysql_fetch_assoc again?
mysql_data_seek($result,mysql_num_rows($result) - 1);
I'm not sure what to use as the second argument. Admittedly, I am not that clear on pointers, but it seems I should clear the pointer to 0. But I get this error:
Offset 0 is invalid for MySQL result index 8 (or the query data is unbuffered
Check your connection with mysql_error() and see if you're getting the "commands out of sync" error. If so, you need to call mysql_free_result() after completing the first query and before starting the second. You could also just call mysql_close() to close your database connection and then reopen a new one.
For more details, see this question.
OP changed the question, so see the edit
*Deleted the posted codes here**
EDIT
After your edited your question and made clear you have actually 2 resources it looks like there is something else wrong. You don't have to worry about pointer when you use two different resources to supply mysql_fetch_assoc(). The thing with mysql_fetch_assoc() is that it takes your param ($result) by reference.
Now to answer your question:
I usually try to minimize calls to MySQL where possible, but I've finally encountered the case where I have to do multiple calls to MySQL in a single script.
Nothing wrong with multiple SQL calls in one script. Although in general you should try to minimize the SQL calls (because they may hurt performance).
It looks like you can't use mysql_fetch_assoc twice in a single script(!).
Plain wrong. Ofc you can do it. As long as you note the above. However when you have two result sets this wouldn't be your problem.
It seems that mysql_data_seek is the solution, but I cannot seem to get it to work.
Again: this has nothing to do with it when you use two (different) result sets.
To be clear, I have two queries I need to make. The second query depends on results from the first.
This shouldn't be any problem at all. It looks like is something else wrong. Have you verified that the second query really is what you think it is? Are you sure there are records? Are you sure there aren't any (MySQL) errors. Do you have error reporting enabled? Have you tried printing out mysql_error()? To better be able to help you can you please provide your real code and not etc etc stuff? Maybe something else is going on.
Or maybe you are simply trying to run the second query inside the first loop. Which would be bad in so many ways.

PHP - Problem with scalar value and memory leak

I am dealing with a very strange situation that has to do with the Warning: Cannot use a scalar value as an array and memory leak.
The script is very simple and I can not figure out the problem.
Code
$variants=array();
if($text)
{
$v=explode(",",$text);
if(is_array($v) && sizeof($v)>0)
{
foreach($v as $i=>$part)
{
$tmp=explode(":",$part);
list($thekey,$thevalue)=$tmp;
//$variants=array();
echo "<div>TYPE==".gettype($variants)."</div>";
echo $variants[$tmp[0]]=$tmp[1];
}
}
}
If I run the code above as stand alone is working fine. But when put it in my framework as small part behave very strange. I got a Warning: Cannot use a scalar value as an array and in order to solve it I added
$variants=array();
on the first line. When running the script the gettype returns ��� the first time and after that return integer.
If i uncomment $variants=array(); just before the gettype, it works. But of course I don't to get the whole array, only the last record return.
I parse my code to find out that the variables I use are declared before I even change all the variable names to stupids but no luck.
Trying to debug and tune the code where times that when running the script instead of see something in the screen the browser download the script instead and some other times I had memory leaks.
Can anyone point where or what to look for, or debug it and solve it?
Problem solved
Before run the code i was calling a function
$obj->draw($$id)
That was causing the problem
The solution
$value=$$id;
$obj->draw($value)
I dont know why this causing the problem.
If anyone has a theory please post it.

Cannot use [] for reading

In one of my scripts, I try to do the following
$data[] = self::get($row['sr_id']); // <-- line 55
However, PHP does not allow me to do this, giving me this error
Fatal error: Cannot use [] for reading in /file.php on line 55
The self::get function return either a bool, or an object.
Edit: The get function creates a new object which again loads data from a mysql database.
The solution in my case was this:
Bad line:
$this->$ExtraTag[] = $fullscript;
Good line:
$this->{$ExtraTag}[] = $fullscript;
or
$this->ExtraTag[] = $fullscript;
Old PHP versions accepted $var[] in expressions, allowed reading out the $var content regardless of syntax. PHP 5.1 made that illegal. But sometimes the error is triggered outside of the intented context.
So my guess (again: show more code) is that the preceeding line contains an unfinished expression, to which the $data[] joins.
In case of object attribute you can wrap your $data var into { }, but that doesn't seem to be the problem in your case. (Else there is something in line 54, that you didn't show.) The right hand side can't reasonably trigger the error. Even array accessing [] an integer or object wouldn't trigger that fatal error.
So if nothing helps, just use array_push(). Work around PHP.
The error I got was:
Fatal error: Cannot use [] for reading in
/pathtosite/drupal/sites/all/themes/zenui/templates/page.tpl.php on line 33
Sometime the problem is when you include a line like this:
$page['sidebar_first'][]
This might happen if you are copying a variable name and forgot to comment out the line.
There was two problems:
1. Missing semicolon
2. $variable[] must set a variable
After fixing these two problems my code read:
$page['sidebar_first'][] = $value;
Don't forget to comment out line you are not using to help with the debugging process
Hope this helps fellow programmers like me!
I had same error with following:
echo implode(',', $array[]);
which should have been
echo implode(',', $array);
Hope this can help someone
try:
$data = Array();
$data[] = self::get($row['sr_id']); // <-- line 55
I had the same problem with my script, the following line threw the same error:
$array[]=$value
I simply replaced it by
$array[count($array)-1]=$value
and it worked perfectly.
Another possible problem could be an accidental double ==.
For example accidentally doing $myArray[] == $myNewValue; would cause this error (because you are trying to read a value with the == instead of telling PHP to assign a new array index).

Categories