PHP Fatal error

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

bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

PHP Fatal error

Post by bouncer »

Hi,

i have some problems with this fatal errors since i update my php version to 5.0.4, and i dont know why this happens:

Code: Select all

$cod = $res[0]['id'];

PHP Fatal error:  Cannot use string offset as an array
this fatal error appears when i use arrays like this $array[0]['something'] and if i use them inside a if statement, if i remove [0] the error doesnt appear anymore, but my page stopps working.

anyone knows whats hapening ?

thanks in advance
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is $res[0]['id'] an array or a string?
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

it's an array
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try declaring $cod as an array before assigning it.

Code: Select all

<?php
$cod = array();
$cod = $res[0]['id'];
?>
Does that throw errors?
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

yes, it throws the same error... :oops:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do a...

Code: Select all

<?php
echo '<pre>'; var_dump($res); echo '</pre>';
?>
And post back what he structure of the array is.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

Code: Select all

array(1) {
  [0]=>
  array(8) {
    ["id"]=>
    string(4) "1272"
    ["data"]=>
    string(10) "2007-04-04"
    ["produtoid"]=>
    string(4) "3069"
    ["codigo"]=>
    string(6) "710089"
    ["preco"]=>
    string(5) "79.90"
    ["descricao"]=>
    string(0) ""
    ["auxiliar"]=>
    string(1) "0"
    ["site"]=>
    string(1) "1"
  }
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

$res[0]['id'] is a string, not an array. This should be a simple assignment.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

so, how can i solve my problem ?

can i use something like this to strings ?

$cod = $res[0]['id'];

thanks in advance
Last edited by bouncer on Wed Apr 04, 2007 12:31 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Echo it. What does it echo? var_dump() it. What does that tell you? It should be a straight assignment.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You can. You've probably posted the wrong bit of code. Post your full script.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

i'm getting problems in $res[0]['id']....

Code: Select all

if(strlen(trim($res[0]['id'])) > 0){
	$pro = $res[0]['ptd'];
	$pre = $res[0]['desc'];
}
if i echo it i get 3026 , and with var_dump() i get string(4) "3026"

what i have to change in $res[0]['id'] ?? (i'm confused :oops: )

thanks in advance
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

I dont know whether it will solve your problem or not, but atleast you can try something like this

Code: Select all

settype($res[0]["id'], "string")
OR

Code: Select all

$res[0]["id"] = (string) $res[0]["id"]
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

thanks mentor for your tip, but the fatal error appears anyway

inside the $res array i have strings and i want to select a specific string, as you can see above, before the update to php(5.0.4) i dont get this kind of error, now i get this error in every part that i want to select a specific string inside an array

thanks in advance
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

(i have missed '') :lol:

if i use this,

Code: Select all

settype($res[0]['id'], "string")
fatal error disappear :) , so i have to use this function every time that i want to select a string form an array ?

if i want to select more than onde string in an array i have to call this function to every one of them ?

thanks in advance
Post Reply