PHP Notice: Uninitialized string offset

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Code: Select all

print_r($dayResult[0]);
Put that directly before the if statement and see what you get.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

superdezign wrote:

Code: Select all

print_r($dayResult[0]);
Put that directly before the if statement and see what you get.
the output for this

Code: Select all

print_r($dayResult[0]);


was

Code: Select all

Array ( [id] => 14 [date] => 2007-07-30 [productID] => 13349 [cod] => 408081 [price] => 389 [description] => [auxiliar] => 0 [site] => 1 )
and for this

Code: Select all

print_r($dayResult);
was

Code: Select all

Array ( [0] => Array ( [id] => 14 [date] => 2007-07-30 [productID] => 13349 [cod] => 408081 [price] => 389 [description] => [auxiliar] => 0 [site] => 1 ) )
regards
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Works fine for me. Obviously, that line is not the problem. Is there anything else on that line? Before? After?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Are you posting the full code? Is there any chance that the 'if' statement is inside a function or anything?
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

this is the code in the main php file,

Code: Select all

$today = date("Y-m-d");
$dayResult = $dbproductDay->returnProductDay($today,$codProd,$_site);
settype($dayResult,"array");  // --> if i dont use this i'll get Fatal error in the if statement 

if($dayResult[0]['productID'] != ""){
	$productDay = $dayResult[0]['cod'];
	$priceDay = $dayResult[0]['price'];
}
and this is returnProductDay function code,

Code: Select all

function returnProductDay($today,$codProd,$_site) {
    $query = "SELECT * FROM productsDay WHERE date = '$date' AND cod = '$codProd' AND site = '$_site'";
    $result = $this->cDb->openCursor($query);
    return $result;
}
regards
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Why the hell would you use settype? Have you ever heard of is_array() or isset()?

And I'm still not getting the same error as you. Is there anything else that you are failing to disclose regarding $dayResult?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

It appears that $result is getting returned as null or an empty string ''.

Are you absolutely 100% positive that you did those print_r()'s and var_dumps()'s with the exact same data/context that was giving you the notice?

Also, I thought you were getting Notices, not fatal errors?
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

Begby wrote:It appears that $result is getting returned as null or an empty string ''.

Are you absolutely 100% positive that you did those print_r()'s and var_dumps()'s with the exact same data/context that was giving you the notice?

Also, I thought you were getting Notices, not fatal errors?
yes, i'm getting notices.

by the way i'm using this statement,

Code: Select all

if ( is_array($dayResult) && ! empty($dayResult) ) {
        $productDay = $dayResult[0]['cod']; 
        $priceDay = $dayResult[0]['price']; 
}
and so far i'm not getting notices :) , do you think that this is a better solution ?

thanks in advance
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

bouncer wrote:

Code: Select all

if ( is_array($dayResult) && ! empty($dayResult) ) {
        $productDay = $dayResult[0]['cod']; 
        $priceDay = $dayResult[0]['price']; 
}
and so far i'm not getting notices :) , do you think that this is a better solution ?
A better solution than settype()? By far. You may also want to check that each of those are set.

Code: Select all

if ( is_array($dayResult) && ! empty($dayResult) && isset($dayResult[0]) ) {
        $productDay = isset($dayResult[0]['cod']) ? $dayResult[0]['cod'] : NULL; 
        $priceDay = isset($dayResult[0]['price']) ? $dayResult[0]['price'] : NULL; 
}
Always ensure that every variable that you use is valid before using it.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

thanks you all :D
Post Reply