...

View Full Version : Error is not being shown, why



jessjenn
01-25-2004, 06:04 AM
I have this method, part of a custom MySQL class, that handles updates, inserts, and deletes, and when there's a problem running mysql_query(), it doesn't return an error. I want the error to be displayed when the argument passed over ($debug) is equal to 1, otherwise, don't show the error on the page. This is my code


// Used for Updates, Insertions, Deletions
function SQLUpdate($sql, $debug=0) {
// Check that it is in fact an UPDATE, INSERT, or DELETE statement
if(eregi("^update |insert |delete ", $sql)) {
if($debug==1) {
$query = mysql_query($sql);
}
else {
@$query = mysql_query($sql);
}

if (!$query) {
echo $this->error_message.$this->sqlupdate_message;
exit;
}
return true;
}
}


Any thoughts? Any help is appreciated. Thanks!

fimi
01-25-2004, 06:08 AM
i think what happens is that the query is right but nothing happends so what i do is check to see if any records were affected by the query(INSERT,UPDATE,DELETE), so do a mysql_affected_rows() test.

firepages
01-25-2004, 08:13 AM
you can check for a MySQL error message by checking the contents of mysql_error(); which will be empty if no error else contain a useful error string.

raf
01-25-2004, 11:04 PM
maybe i just don't understand it, but you have


function SQLUpdate($sql, $debug=0) {
if($debug==1) {

Does that mean that $debug==1 can never be True ?


Fimi:
i think what happens is that the query is right but nothing happends so what i do is check to see if any records were affected by the query(INSERT,UPDATE,DELETE), so do a mysql_affected_rows() test.
I don't see the relevance of affected rows here.
If the query is syntactically correct, then the mysql server will execute it. So no error should be returned then.

But that has nothing to do with the number of affected rows !
If you have
delete from table where id=999999999999999999999
then mysql_affected_rows will probably be 0, because there are no records with that id- value
Or if you run an update on an existing record, but all new values are the same as the old ones, then you will get a 0, since no row was affected.

If you want to know if there was a problem executing the querys, then you don't need a check at all. Since mysql_query() returns False is there was an error when executing the statement.
so
if (!$query) {
is sufficient as a chack. You just need to include the mysql_errno() or mysql_error() inside your feedback.


jessjenn,

you might wanna add 'replace' as querytype to this function ...

jessjenn
01-25-2004, 11:37 PM
Originally posted by raf
maybe i just don't understand it, but you have


function SQLUpdate($sql, $debug=0) {
if($debug==1) {

Does that mean that $debug==1 can never be True ?

Well, it will always be false unless you use an argument of "1" - for example:

SQLUpdate("update...") -> will not return an from MySQL error
SQLUpdate("update...",1) -> wil return an error
SQLUpdate("update...", 9999999) -> will not return an error because it's not equal 1

Thanks for all your suggestions folks. I will try them out. Thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum