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: [email protected]
-----Original Message-----
From: Tim Legg [mailto:[email protected]]
Sent: Wednesday, September 23, 2009 11:42 PM
To: [email protected]
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