Stricter Error Checking?

Stricter Error Checking?

am 23.09.2009 20:11:46 von Tim Legg

Hello,

I just spent way, way to much time trying to debug code due to a misnamed element. Here is a simplified example of the problem I dealt with.


$test = "SELECT * FROM `Materials` WHERE `Part_Number` = '125664'";
$result = mysql_query($test,$handle);
if(!$result)
{
die('Error: ' . mysql_error());
}
$row = mysql_fetch_array($result);
echo $row['Number'];

After retyping the code 3 or 4 times over the course of the morning, I finally found where the problem was. The problem is that the database field is called 'Part_Number', not 'Number'. The field 'Number' does not exist in the database. I am very surprised that I didn't even get a warning that there might be a problem with the statement. All I saw is that nothing was being returned via the echo command.

There really must be a stricter error checking that is turned on somewhere, isn't there?


Thanks for your help.

Tim




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Stricter Error Checking?

am 23.09.2009 20:19:08 von Jonathan Tapicer

Hi,

There is, see here (or it can also be set through php.ini):

http://www.php.net/manual/en/function.error-reporting.php

You are looking for E_STRICT.

Regards,

Jonathan

On Wed, Sep 23, 2009 at 3:11 PM, Tim Legg wrote:
> Hello,
>
> I just spent way, way to much time trying to debug code due to a misnamed=
element. =A0Here is a simplified example of the problem I dealt with.
>
>
> =A0 =A0 =A0 =A0$test =3D "SELECT * FROM `Materials` WHERE `Part_Number` =
=3D '125664'";
> =A0 =A0 =A0 =A0$result =3D mysql_query($test,$handle);
> =A0 =A0 =A0 =A0if(!$result)
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0die('Error: ' . mysql_error());
> =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0$row =3D mysql_fetch_array($result);
> =A0 =A0 =A0 =A0echo $row['Number'];
>
> After retyping the code 3 or 4 times over the course of the morning, I fi=
nally found where the problem was. =A0The problem is that the database fiel=
d is called 'Part_Number', not 'Number'. =A0The field 'Number' does not exi=
st in the database. =A0I am very surprised that I didn't even get a warning=
that there might be a problem with the statement. =A0All I saw is that not=
hing was being returned via the echo command.
>
> There really must be a stricter error checking that is turned on somewher=
e, isn't there?
>
>
> Thanks for your help.
>
> Tim
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Stricter Error Checking?

am 23.09.2009 20:28:09 von Tommy Pham

----- Original Message ----
> From: Tim Legg
> To: php-general@lists.php.net
> Sent: Wednesday, September 23, 2009 11:11:46 AM
> Subject: [PHP] Stricter Error Checking?
>
> Hello,
>
> I just spent way, way to much time trying to debug code due to a misnamed
> element. Here is a simplified example of the problem I dealt with.
>
>
> $test = "SELECT * FROM `Materials` WHERE `Part_Number` = '125664'";
> $result = mysql_query($test,$handle);
> if(!$result)
> {
> die('Error: ' . mysql_error());
> }
> $row = mysql_fetch_array($result);
> echo $row['Number'];
>
> After retyping the code 3 or 4 times over the course of the morning, I finally
> found where the problem was. The problem is that the database field is called
> 'Part_Number', not 'Number'. The field 'Number' does not exist in the
> database. I am very surprised that I didn't even get a warning that there might
> be a problem with the statement. All I saw is that nothing was being returned
> via the echo command.

if(!$result)
{
die('Error: ' . mysql_error());
}

This didn't work when you used 'Number' instead of 'Part_Number'? Strange...

>
> There really must be a stricter error checking that is turned on somewhere,
> isn't there?
>
>
> Thanks for your help.
>
> Tim
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Stricter Error Checking?

am 23.09.2009 20:31:40 von Jonathan Tapicer

I think he meant that he is using 'Number' instead of 'Part_Number'
when accessing the array and not in the SQL, his SQL was correct, this
was wrong:

> echo $row['Number'];

E_STRICT catches that kind of error.

Jonathan

On Wed, Sep 23, 2009 at 3:28 PM, Tommy Pham wrote:
>
>
> ----- Original Message ----
>> From: Tim Legg
>> To: php-general@lists.php.net
>> Sent: Wednesday, September 23, 2009 11:11:46 AM
>> Subject: [PHP] Stricter Error Checking?
>>
>> Hello,
>>
>> I just spent way, way to much time trying to debug code due to a misname=
d
>> element. =A0Here is a simplified example of the problem I dealt with.
>>
>>
>> =A0 =A0 $test =3D "SELECT * FROM `Materials` WHERE `Part_Number` =3D '12=
5664'";
>> =A0 =A0 $result =3D mysql_query($test,$handle);
>> =A0 =A0 if(!$result)
>> =A0 =A0 {
>> =A0 =A0 =A0 =A0 die('Error: ' . mysql_error());
>> =A0 =A0 }
>> =A0 =A0 $row =3D mysql_fetch_array($result);
>> =A0 =A0 echo $row['Number'];
>>
>> After retyping the code 3 or 4 times over the course of the morning, I f=
inally
>> found where the problem was. =A0The problem is that the database field i=
s called
>> 'Part_Number', not 'Number'. =A0The field 'Number' does not exist in the
>> database. =A0I am very surprised that I didn't even get a warning that t=
here might
>> be a problem with the statement. =A0All I saw is that nothing was being =
returned
>> via the echo command.
>
> if(!$result)
> {
> =A0 =A0 =A0 =A0die('Error: ' . mysql_error());
> }
>
> This didn't work when you used 'Number' instead of 'Part_Number'? Strange=
....
>
>>
>> There really must be a stricter error checking that is turned on somewhe=
re,
>> isn't there?
>>
>>
>> Thanks for your help.
>>
>> Tim
>>
>>
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Stricter Error Checking?

am 23.09.2009 20:37:00 von Bob McConnell

From: Tommy Pham
>> From: Tim Legg
>>=20
>> I just spent way, way to much time trying to debug code due to a
misnamed=20
>> element. Here is a simplified example of the problem I dealt with.
>>=20
>>=20
>> $test =3D "SELECT * FROM `Materials` WHERE `Part_Number` =3D
'125664'";
>> $result =3D mysql_query($test,$handle);
>> if(!$result)
>> {
>> die('Error: ' . mysql_error());
>> }
>> $row =3D mysql_fetch_array($result);
>> echo $row['Number'];
>>=20
>> After retyping the code 3 or 4 times over the course of the morning,
I finally=20
>> found where the problem was. The problem is that the database field
is called=20
>> 'Part_Number', not 'Number'. The field 'Number' does not exist in
the=20
>> database. I am very surprised that I didn't even get a warning that
there might=20
>> be a problem with the statement. All I saw is that nothing was being
returned=20
>> via the echo command.
>=20
> if(!$result)
> {
> die('Error: ' . mysql_error());
> }
>=20
> This didn't work when you used 'Number' instead of 'Part_Number'?
Strange...
>=20

I think the problem is that he didn't check that the key he used
actually existed before using the value it pointed to. So he got an
empty string for $row['Number']; because the key should have been
'Part_Number'. I don't know that even E_STRICT would catch that one.

Bob McConnell

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Stricter Error Checking?

am 23.09.2009 20:45:50 von Tommy Pham

----- Original Message ----
> From: Bob McConnell
> To: Tommy Pham ; php-general@lists.php.net
> Sent: Wednesday, September 23, 2009 11:37:00 AM
> Subject: RE: [PHP] Stricter Error Checking?
>
> From: Tommy Pham
> >> From: Tim Legg
> >>
> >> I just spent way, way to much time trying to debug code due to a
> misnamed
> >> element. Here is a simplified example of the problem I dealt with.
> >>
> >>
> >> $test = "SELECT * FROM `Materials` WHERE `Part_Number` =
> '125664'";
> >> $result = mysql_query($test,$handle);
> >> if(!$result)
> >> {
> >> die('Error: ' . mysql_error());
> >> }
> >> $row = mysql_fetch_array($result);
> >> echo $row['Number'];
> >>
> >> After retyping the code 3 or 4 times over the course of the morning,
> I finally
> >> found where the problem was. The problem is that the database field
> is called
> >> 'Part_Number', not 'Number'. The field 'Number' does not exist in
> the
> >> database. I am very surprised that I didn't even get a warning that
> there might
> >> be a problem with the statement. All I saw is that nothing was being
> returned
> >> via the echo command.
> >
> > if(!$result)
> > {
> > die('Error: ' . mysql_error());
> > }
> >
> > This didn't work when you used 'Number' instead of 'Part_Number'?
> Strange...
> >
>
> I think the problem is that he didn't check that the key he used
> actually existed before using the value it pointed to. So he got an
> empty string for $row['Number']; because the key should have been
> 'Part_Number'. I don't know that even E_STRICT would catch that one.
>
> Bob McConnell
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

My mistake, didn't have enough caffeine for the day :)


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Stricter Error Checking?

am 23.09.2009 20:46:46 von Shawn McKenzie

Bob McConnell wrote:
> From: Tommy Pham
>>> From: Tim Legg
>>>
>>> I just spent way, way to much time trying to debug code due to a
> misnamed
>>> element. Here is a simplified example of the problem I dealt with.
>>>
>>>
>>> $test = "SELECT * FROM `Materials` WHERE `Part_Number` =
> '125664'";
>>> $result = mysql_query($test,$handle);
>>> if(!$result)
>>> {
>>> die('Error: ' . mysql_error());
>>> }
>>> $row = mysql_fetch_array($result);
>>> echo $row['Number'];
>>>
>>> After retyping the code 3 or 4 times over the course of the morning,
> I finally
>>> found where the problem was. The problem is that the database field
> is called
>>> 'Part_Number', not 'Number'. The field 'Number' does not exist in
> the
>>> database. I am very surprised that I didn't even get a warning that
> there might
>>> be a problem with the statement. All I saw is that nothing was being
> returned
>>> via the echo command.
>> if(!$result)
>> {
>> die('Error: ' . mysql_error());
>> }
>>
>> This didn't work when you used 'Number' instead of 'Part_Number'?
> Strange...
>
> I think the problem is that he didn't check that the key he used
> actually existed before using the value it pointed to. So he got an
> empty string for $row['Number']; because the key should have been
> 'Part_Number'. I don't know that even E_STRICT would catch that one.
>
> Bob McConnell

What? With E_ALL or E_STRICT:

Notice: Undefined index: Number in file.php on line X

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Stricter Error Checking?

am 23.09.2009 20:49:16 von Shawn McKenzie

>> I think the problem is that he didn't check that the key he used
>> actually existed before using the value it pointed to. So he got an
>> empty string for $row['Number']; because the key should have been
>> 'Part_Number'. I don't know that even E_STRICT would catch that one.
>>
>> Bob McConnell
>
> What? With E_ALL or E_STRICT:
>
> Notice: Undefined index: Number in file.php on line X
>

My bad, E_ALL only.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Stricter Error Checking?

am 23.09.2009 20:54:40 von Shawn McKenzie

Tim Legg wrote:
> Hello,
>
> I just spent way, way to much time trying to debug code due to a misnamed element. Here is a simplified example of the problem I dealt with.
>
>
> $test = "SELECT * FROM `Materials` WHERE `Part_Number` = '125664'";
> $result = mysql_query($test,$handle);
> if(!$result)
> {
> die('Error: ' . mysql_error());
> }
> $row = mysql_fetch_array($result);
> echo $row['Number'];
>
> After retyping the code 3 or 4 times over the course of the morning, I finally found where the problem was. The problem is that the database field is called 'Part_Number', not 'Number'. The field 'Number' does not exist in the database. I am very surprised that I didn't even get a warning that there might be a problem with the statement. All I saw is that nothing was being returned via the echo command.
>
> There really must be a stricter error checking that is turned on somewhere, isn't there?
>
>
> Thanks for your help.
>
> Tim
>
>
>

When developing you should always use something like this:

ini_set('display_errors', 'On');
error_reporting(E_ALL);

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Stricter Error Checking?

am 23.09.2009 22:35:53 von List Manager

Tim Legg wrote:
> Hello,
>
> I just spent way, way to much time trying to debug code due to a misnamed element. Here is a simplified example of the problem I dealt with.
>
>
> $test = "SELECT * FROM `Materials` WHERE `Part_Number` = '125664'";
> $result = mysql_query($test,$handle);
> if(!$result)
> {
> die('Error: ' . mysql_error());
> }
> $row = mysql_fetch_array($result);
> echo $row['Number'];
>
> After retyping the code 3 or 4 times over the course of the morning, I finally found where the problem was. The problem is that the database field is called 'Part_Number', not 'Number'. The field 'Number' does not exist in the database. I am very surprised that I didn't even get a warning that there might be a problem with the statement. All I saw is that nothing was being returned via the echo command.
>
> There really must be a stricter error checking that is turned on somewhere, isn't there?
>
>
> Thanks for your help.
>
> Tim
>
>
>
>

This would generate a E_NOTICE level "warning"

to see the errors of you ways you would need the following at the top of your
script, or set in your php.ini file or in a .htaccess file.


In script example:

error_reporting(E_ALL);
ini_set('display_errors', 1);

....

?>

..htaccess & php.ini example:

error_reporting = E_ALL
display_errors = On



The default setting in PHP

error_reporting = E_ALL & ~E_NOTICE
display_errors = On



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Stricter Error Checking?

am 24.09.2009 07:08:19 von Samrat Kar

Better use mysqli rather mysql. It supports all new features of MySQL.

I solved the problem in following manner.

1. Detect the return value of mysqli->query function.
2. If it is FALSE get the error no, error msg etc and store in a Mysql table
for further review. You can also write it in a error log file.

Example...

/* Part of MYSQLI class */

public function Query($sql, $flag = 0) {
$my = new mysqli($this->host,$this->user,$this->pass,$this->db);
if(mysqli_connect_errno()) {
$this->conn_err_no = mysqli_connect_errno();
$this->conn_err_msg = mysqli_connect_error();
return FALSE;
}
//$this->QueryLog(addslashes($sql));
$arr1 = explode(" ", $sql);
$queryType = $arr1[0];
$result = $my->query($sql);
if($result === FALSE) {
$this->query_err_no = $my->errno;
$this->query_err_msg = $my->error;
$this->sqlstate = $my->sqlstate;
$this->ErrorLog(addslashes($sql));
}
else {
$this->QueryLog(addslashes($sql));
}

if($queryType == "SELECT" || $queryType == "SHOW" || $queryType ==
"DESCRIBE" || $queryType == "EXPLAIN" || $queryType == "CALL") {
if($result) {
$num_row = $result->num_rows;
$num_col = $result->field_count;
if($num_row == 0)
$ret = NULL;
if($num_row == 1 && $num_col == 1) {
$record = $result->fetch_row();
if($flag == 1)
$ret = array($record[0]);
else
$ret = $record[0];
}
if($num_row == 1 && $num_col > 1) {
if($flag == 1)
$ret = array($result->fetch_array());
else
$ret = $result->fetch_row();
}
if($num_row > 1 && $num_col == 1) {
$retarr = array();
while($record = $result->fetch_array()) {
array_push($retarr, $record[0]);
}
$ret = $retarr;
}
if($num_row > 1 && $num_col > 1) {
$retarr = array();
while($record = $result->fetch_array()) {
array_push($retarr, $record);
}
$ret = $retarr;
}
if($num_row > 0) {
$this->field_list = $result->fetch_fields();
}
}
else {
return FALSE;
}
}
else {
if($result === FALSE)
return FALSE;
if($queryType == "UPDATE" || $queryType == "INSERT" ||
$queryType == "REPLACE" || $queryType == "DELETE") {
$ret = $my->affected_rows;
if($queryType == "INSERT")
$this->last_insert_id = $my->insert_id;
}
}

return $ret;
}

private function QueryLog($query) {
$my = new mysqli($this->host,$this->user,$this->pass,$this->db);
if(mysqli_connect_errno()) {
$this->conn_err_no = mysqli_connect_errno();
$this->conn_err_msg = mysqli_connect_error();
return FALSE;
}
$ts = time();
$sql = "INSERT INTO pi_global.tblquerylog
VALUES('','$ts','$query')";
$my->query($sql);
$my->close();
}

private function ErrorLog($query) {
$my = new mysqli($this->host,$this->user,$this->pass,$this->db);
if(mysqli_connect_errno()) {
$this->conn_err_no = mysqli_connect_errno();
$this->conn_err_msg = mysqli_connect_error();
return FALSE;
}
$ts = time();
$errno = $this->query_err_no;
$errmsg = addslashes($this->query_err_msg);
$state = $this->sqlstate;
$sql = "INSERT INTO pi_global.tblerrorlog
VALUES('','$ts','$query','$errno','$errmsg','$state')";
$my->query($sql);
$my->close();
}

Regards,

Samrat Kar
FRD, BARC

Tel: 022-25597295
Alternate Email: esamrat@yahoo.com

-----Original Message-----
From: Tim Legg [mailto:kc0ofc@yahoo.com]
Sent: Wednesday, September 23, 2009 11:42 PM
To: php-general@lists.php.net
Subject: [PHP] Stricter Error Checking?

Hello,

I just spent way, way to much time trying to debug code due to a misnamed
element. Here is a simplified example of the problem I dealt with.


$test = "SELECT * FROM `Materials` WHERE `Part_Number` = '125664'";
$result = mysql_query($test,$handle);
if(!$result)
{
die('Error: ' . mysql_error());
}
$row = mysql_fetch_array($result);
echo $row['Number'];

After retyping the code 3 or 4 times over the course of the morning, I
finally found where the problem was. The problem is that the database field
is called 'Part_Number', not 'Number'. The field 'Number' does not exist in
the database. I am very surprised that I didn't even get a warning that
there might be a problem with the statement. All I saw is that nothing was
being returned via the echo command.

There really must be a stricter error checking that is turned on somewhere,
isn't there?


Thanks for your help.

Tim




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.409 / Virus Database: 270.13.112/2391 - Release Date: 09/23/09
18:00:00



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php