Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error is not being shown, why

    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

    PHP 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!

  • #2
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by fimi; 01-25-2004 at 07:14 AM.

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    maybe i just don't understand it, but you have
    PHP Code:
    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 ...

  • #5
    New Coder
    Join Date
    Dec 2003
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by raf
    maybe i just don't understand it, but you have
    PHP Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •