PHP Fatal error
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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
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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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.
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
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
here's another code that give me fatal error first i have two functions in client.obj.php called verificaLogin and verificaSessao,
then i call them somewhere in the code of login.php, client is the name of class where the functions are,
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)
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;
}
}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
..."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)
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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;
}
?>as i expected the problem is in abreCursor function, because i dont get anything from $res in those 2 functions above i only get:
there is something wrong with this functions and i dont get it
because $res is empty somehow.
Code: Select all
string(0) ""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;
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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;
}
?>thanks for the example
, but there is something wrong ... my $res inside the 2 functions verificaLogin and verificaSessao is always empty,
some idea how to handle this ?
Code: Select all
string(0) ""- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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 ....
or something not right here ....
if i use this in another place it dont work
, so probably is something wrong with this.....
thanks in advance
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;
}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;
}thanks in advance
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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;
}
?>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) ""in the top of the classWhere is the cDb object instantied inside 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);
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA