Page 3 of 5

Posted: Thu Apr 05, 2007 2:29 pm
by onion2k
No. Everah's script demonstrates that PHP is fine and that the problem lies somewhere within your code.

Posted: Thu Apr 05, 2007 3:50 pm
by RobertGonzalez
bouncer wrote:it's possible that this errors are related with php config ??
Everah wrote:If there were a problem with PHP it would have errored in the script the same as in your script.
Your PHP installation is fine. Your code is not.

Posted: Sat Apr 07, 2007 5:32 am
by bouncer
maybe i just have to make some review to code...

if give me fatal error in a line like this one $res[0]['something'] i change it to $res['something'] with this, the error disappear but i dont have 100% sure that this is correct, at least it works fine, but i have places that i use $res[$i]['something'] inside a loop and i dont know how to change that...

can you tell me if i have to declare the global vars in php 5.0.4, and constants ?

thanks in advance

Posted: Sat Apr 07, 2007 9:20 am
by RobertGonzalez
Not quite sure I follow you here, but you must declare global vars in order to bring them in the right scope, unless you pass them to the functions/methods. Constants are global by default and do not need to be declared.

I suspect there is more going on in your code that is causing your problem. It might be helpful if we can see, from start to finish, how that $res var is set and handled.

Posted: Mon Apr 09, 2007 5:22 am
by bouncer
here is some information that i found related with my problem ( Backward Incompatible Changes in Migration from PHP 4 to PHP 5 ) in php.net, in point 3.

http://php.mirror.camelnetwork.com/manu ... atible.php

http://bugs.php.net/bug.php?id=31303

today i will post here my function where i populate my $res var and some related code, so you can see it a give me some tips ...

thanks in advance

Posted: Mon Apr 09, 2007 9:00 am
by bouncer
here's another code that give me fatal error first i have two functions in client.obj.php called verificaLogin and verificaSessao,

Code: Select all

function verificaLogin($usr, $pwd) {
     $query = "select * from clients where username='$usr' and password='$pwd' and activo=1 and revenda=1";
     $res = $this->cDb->abreCursor($query);
     return $res;
}

function verificaSessao($name) {
     $query = "select * from sessao where user='$name'";
     $sres= $this->cDb->abreCursor($query);
     if ($sres["user"]==$name){
	$query="delete from sessao where user='$name'";
	$dres = $this->cDb->executaQuery($query);
	$query = "select * from clients where username='$name' and activo=1 and revenda=1";
	$res = $this->cDb->abreCursor($query);
	return $res;
     }else{
                return 0;
     }
}
then i call them somewhere in the code of login.php, client is the name of class where the functions are,

Code: Select all

$dbclient = new client();
		
if ($pid) {
     $ver = $dbclient->verificaSessao($pid);
} else {
     $ver = $dbclient->verificaLogin($usr, $newpwd);
}
		
if (strlen($ver[0]["name"]) == 0) { $erro = 1; }      <-- line 28
...
note that this code is different from the other that i gave but the use of $ver[0]["name"] is the same, then i get the following error:

"PHP Fatal error: Cannot use string offset as an array in html/revenda/login.php on line 28"

i think that the problem is in the abreCursor function, dont have sure, can you take a look at it ?.... (2nd page of this topic)

Posted: Mon Apr 09, 2007 11:20 am
by RobertGonzalez
Change your functions to this just for testing and see what is echoed to the screen:

Code: Select all

<?php
function verificaLogin($usr, $pwd) {
    $query = "select * from clients where username='$usr' and password='$pwd' and activo=1 and revenda=1";
    $res = $this->cDb->abreCursor($query);
    echo '<pre>'; var_dump($res); echo '</pre>';
    return $res;
}

function verificaSessao($name) {
    $query = "select * from sessao where user='$name'";
    $sres= $this->cDb->abreCursor($query);
    if ($sres["user"]==$name){
        $query="delete from sessao where user='$name'";
        $dres = $this->cDb->executaQuery($query);
        $query = "select * from clients where username='$name' and activo=1 and revenda=1";
        $res = $this->cDb->abreCursor($query);
        echo '<pre>'; var_dump($res); echo '</pre>';
        return $res;
     }
     
     return 0;
}
?>

Posted: Mon Apr 09, 2007 11:35 am
by bouncer
as i expected the problem is in abreCursor function, because i dont get anything from $res in those 2 functions above i only get:

Code: Select all

string(0) ""
there is something wrong with this functions and i dont get it :(

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) {

				$this->result = array(); 
				while($this->result[] = mysql_fetch_assoc($this->query)); 
				array_pop($this->result);

                        } else {

				$this->result = "";
				$this->varerrorid = 1;
				$this->varerrordesc = mysql_error();

                       }

                } else {

			$this->result = "";
			$this->varerrorid = 1;
			$this->varerrordesc = mysql_error();

                }
                return $this->result;
        }
because $res is empty somehow.

Posted: Mon Apr 09, 2007 11:47 am
by RobertGonzalez
I am not sure if this is what you are after, but it is worth a try:

Code: Select all

<?php
function abreCursor ($query) {
    // This should be to null as a class var
    $this->result = "";
    
    // Call the query and handle errors
    if (($this->query = mysql_query($query)) === false) {
        $this->varerrorid = 1;
        $this->varerrordesc = mysql_error();
    } else {
        $this->varerrorid = 0;
        $this->numberrows;
        if (($this->numberrows = mysql_num_rows($this->query)) > 0) {
            $this->result = array();
            while ($row = mysql_fetch_assoc($this->query)) {
                $this->result[] = $row;
            }
            array_pop($this->result);
    }

    return $this->result;
}
?>

Posted: Mon Apr 09, 2007 11:58 am
by bouncer
thanks for the example :D , but there is something wrong ... my $res inside the 2 functions verificaLogin and verificaSessao is always empty,

Code: Select all

string(0) ""
some idea how to handle this ?

Posted: Mon Apr 09, 2007 12:17 pm
by RobertGonzalez
You are going to have to do some debugging by yourself on this one. More than likely there is something screwey in your queries and returns from them. Try som error checking or vardump()ing along the way. Also, since the functions are only a few lines, you could echo out each step and see what is executing and not executing.

Posted: Mon Apr 09, 2007 1:14 pm
by bouncer
i found the problem, but i dont know what i'm doing wrong in here and dont have 100% sure if the problem is in abreCursor() or in verificaLogin(), i have change my abreCursor function with the one that you provided and it continues not working...

if i run the query outside the function it works fine....

the values for $usr and $pwd are right ....

Code: Select all

function verificaLogin($usr, $pwd) { 
    $query = "select * from clients where username='$usr' and password='$pwd' and activo=1 and revenda=1"; 
    $res = $this->cDb->abreCursor($query); 
    return $res; 
}
or something not right here ....

Code: Select all

function abreCursor ($query) { 
    // This should be to null as a class var 
    $this->result = ""; 
    
    // Call the query and handle errors 
    if (($this->query = mysql_query($query)) === false) { 
        $this->varerrorid = 1; 
        $this->varerrordesc = mysql_error(); 
    } else { 
        $this->varerrorid = 0; 
        $this->numberrows; 
        if (($this->numberrows = mysql_num_rows($this->query)) > 0) { 
            $this->result = array(); 
            while ($row = mysql_fetch_assoc($this->query)) { 
                $this->result[] = $row; 
            } 
            array_pop($this->result); 
    } 

    return $this->result; 
}
if i use this in another place it dont work :wink: , so probably is something wrong with this.....

thanks in advance

Posted: Mon Apr 09, 2007 1:44 pm
by RobertGonzalez
Just for the sake of testing, replace your current functions with these. I think it has something to do with how the abreCursor method is called. Where is the cDb object instantied inside the class?

Code: Select all

<?php
function verificaLogin($usr, $pwd) {
    // Delete all echoes when done testing
    echo '<h2>Testing...</h2>';
    $query = "select * from clients where username='$usr' and password='$pwd' and activo=1 and revenda=1";
    echo '<p>The query is ' . $query . '<br />';
     
    $res = $this->cDb->abreCursor($query);
    echo '<pre>'; var_dump($this->cDb); echo '</pre>';
    echo '<pre>'; var_dump($res); echo '</pre>';
    return $res;
}

function abreCursor ($query) {
    // This should be to null as a class var
    $this->result = "";
    echo '<h2>Testing inside of abreCursor</h2>';
    // Call the query and handle errors
    if (($this->query = mysql_query($query)) === false) {
        $this->varerrorid = 1;
        $this->varerrordesc = mysql_error();
        echo '<p>' . $this->varerrordesc . ' is the mysql error description<br />';
    } else {
        $this->varerrorid = 0;
        $this->numberrows;
        if (($this->numberrows = mysql_num_rows($this->query)) > 0) {
            $this->result = array();
            while ($row = mysql_fetch_assoc($this->query)) {
                $this->result[] = $row;
            }
            echo '<pre>'; var_dump($this->result); echo '</pre>';
            array_pop($this->result);
            echo '<pre>'; var_dump($this->result); echo '</pre>';
    }
    echo '<pre>'; var_dump($this->result); echo '</pre>';
    return $this->result;
} 
?>

Posted: Tue Apr 10, 2007 3:39 am
by bouncer
this is what i get ....

Code: Select all

Testing inside of abreCursor
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 is the mysql error description


Testing inside of abreCursor
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 is the mysql error description


Testing...
The query is select * from clients where username='test' and password='~1#@4&' and activo=1 and revenda=1


Testing inside of abreCursor
string(0) ""

object(db)#7 (6) {
  ["descriptor"]=>
  resource(8) of type (mysql link)
  ["db"]=>
  string(11) "incom"
  ["result"]=>
  string(0) ""
  ["query"]=>
  resource(12) of type (mysql result)
  ["varerrorid"]=>
  int(0)
  ["numberrows"]=>
  int(0)
}

string(0) ""
Where is the cDb object instantied inside the class?
in the top of the class

Code: Select all

class clients {
     var $cDb;
     var $cConfig;
     var $retorno;

     function clients () {
          $cConfig = new config();
          $this->cDb = new db();
          $this->con=$this->cDb->abreConexao($cConfig->db, $cConfig->login, $cConfig->senha,$cConfig->odbc,$cConfig->driver,$cConfig->servidor);
     }

Posted: Tue Apr 10, 2007 10:25 am
by RobertGonzalez
Notice the SQL error reports up there?