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

Post by bouncer »

if i use this, it works fine

Code: Select all

if(strlen(trim($res['id'])) > 0){ 
   $pro = $res['ptd']; 
   $pre = $res['desc']; 
}
but if i have a loop like this,

Code: Select all

for($i=$minrec; $i<=$maxrec; $i++) {
     $image = $result[$i]["img_p"];
     if ( $image != "" ) {
          $image = $result[$i]["img_p"];
     }else{
          $image = "/img/base/_blank_peq.gif";
     }
}
gives the same error in line 2,4, how can i resolve the problem in this case ?

thanks in advance
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

is $result an array? Just because you used $res before

Lets see the code that sets it.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

$result and $res are different arrays.....

Code: Select all

$result = mysql_query("SELECT * FROM productDetails);
now for each record i want to show some info....

thanks in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

With that code $result will never be an array. A resource identifier or a boolean is the best it could hope to be.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

Code: Select all

function devolveAntiLeiloes(){
$sql = "SELECT *,a.data as data, a.activo as activo, a.id as id, b.codigo as codigo, a.preco as preco, a.id_cliente  as id_cliente, b.id as cod_prod FROM antileilao a inner join produtos b on a.id_produto=b.codigo where a.mostra=1  and a.activo=1 and CURDATE()<=a.datafim order by a.data";
        $res = $this->cDb->abreCursor($sql);
        return $res;   
    }

Code: Select all

$dbantileilao = new antileilao();
$result = $dbantileilao->devolveAntiLeiloes();

for($i=$minrec; $i<=$maxrec; $i++) {
     $image = $result[$i]["img_p"];
     if ( $image != "" ) {
          $image = $result[$i]["img_p"];
     }else{
          $image = "/img/base/_blank_peq.gif";
     }
...
this is the code related....and it works fine until php version 5.0.4, always get Fatal Error in $result[$i]["img_p"]

thanks in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What does "abreCursor()" do?
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

feyd wrote:What does "abreCursor()" do?
it's a function to run a query, like mysql_query(), and see if there are any errors, if you want to see the function code i can put it here.... :wink:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

In order to help you, we'll need to see what it does...
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

Code: Select all

function abreCursor ($query) {
     $this->result = "";
     $this->query = mysql_query($query);
     if ( strlen ( trim ( mysql_error() ) ) ==  0 ) {
          $this->varerrorid = 0;
          $this->numberrows = mysql_num_rows($this->query);
          if ($this->numberrows > 0) {
               for ($x = 0; $row = mysql_fetch_row($this->query); $x++) { 
                    $nf = mysql_num_fields($this->query); 
	    for ($w = 0; $w < $nf; $w++) {
                         $this->result[$x][mysql_field_name($this->query, $w)] = $row[$w]
	    }
               }
          } else {
                $this->result = "";
	$this->varerrorid = 1;
                $this->varerrordesc = mysql_error();
          }
     } else {
          $this->result = "";
          $this->varerrorid = 1;
          $this->varerrordesc = mysql_error();
     }
     return $this->result;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your method has the potential to return an empty string or an array. Why not always return an array (empty or otherwise)?

Also, the following can be simplified

Code: Select all

for ($x = 0; $row = mysql_fetch_row($this->query); $x++) {
                    $nf = mysql_num_fields($this->query);
            for ($w = 0; $w < $nf; $w++) {
                         $this->result[$x][mysql_field_name($this->query, $w)] = $row[$w]
            }
               }
to

Code: Select all

$this->result = array();
while($this->result[] = mysql_fetch_assoc($this->query));
array_pop($this->result);
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

now that you've mentioned it, i'll have to review that.... :wink:

but i think that "PHP Fatal error: Cannot use string offset as an array" it's not related with the code above, and i don't know why i'm getting this error

btw 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 »

Run this all by itself in a new file and see if your get the same error. In fact, post back exactly what comes to the screen.

Code: Select all

<html>
<head>
<title>Array Testing</title>
</head>

<body>
<?php
$res = array(
	array(
		"id" 		=> "1272",
		"data" 		=> "2007-04-04",
		"produtoid" => "3069",
		"codigo"	=> "710089",
		"preco"		=> "79.90",
		"descricao"	=> "",
		"auxiliar"	=> "0",
		"site"		=> "1"
	)
);
clean_dump($res);
$cod = $res[0]['id'];
echo $cod;

function clean_dump($var)
{
	echo '<pre>'; var_dump($var); echo '</pre>';
}
?>
</body>
</html>
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

Output:

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" } } 1272
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So if there are no errors in the script I posted then there are errors somewhere in the building of the $res array. I took your var_dump and used it exactly as you posted it, then assigned it exactly as you assigned it. If there were a problem with PHP it would have errored in the script the same as in your script.

You may need to debug further up the code to see what is happening.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

it's possible that this errors are related with php config ??

thanks in advance
Post Reply