Page 2 of 5

Posted: Thu Apr 05, 2007 5:01 am
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

Posted: Thu Apr 05, 2007 7:00 am
by mikeq
is $result an array? Just because you used $res before

Lets see the code that sets it.

Posted: Thu Apr 05, 2007 8:44 am
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

Posted: Thu Apr 05, 2007 8:58 am
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.

Posted: Thu Apr 05, 2007 9:09 am
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

Posted: Thu Apr 05, 2007 9:23 am
by feyd
What does "abreCursor()" do?

Posted: Thu Apr 05, 2007 9:32 am
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:

Posted: Thu Apr 05, 2007 9:35 am
by feyd
In order to help you, we'll need to see what it does...

Posted: Thu Apr 05, 2007 9:44 am
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;
}

Posted: Thu Apr 05, 2007 9:52 am
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);

Posted: Thu Apr 05, 2007 10:05 am
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

Posted: Thu Apr 05, 2007 10:23 am
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>

Posted: Thu Apr 05, 2007 10:42 am
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

Posted: Thu Apr 05, 2007 12:47 pm
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.

Posted: Thu Apr 05, 2007 1:24 pm
by bouncer
it's possible that this errors are related with php config ??

thanks in advance