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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts

    error reporting being called even on successfull query.

    hey everyone,

    i have this function in my class that i am developing. its a mysql wrapper class. (i know there are lots of these around, but i generally like to write my own code, and since oop in php is new to me, there are lots of examples to learn from :P)...

    this is the function in question.

    PHP Code:
        function sql_query($query) {
        
            
    $this->query $query;
            
            
    $this->result = @mysql_query($query$this->link) or $this->error(mysql_error(), $query);
                
            
    $count $this->query_count;
            
    $count++;
            
    $this->query_count $count;
        
        } 
    being called like so...

    PHP Code:
        require("includes/config.php");
        require(
    "includes/class/class.mysql.php");

        
    $db = new mysql(DB_HOSTDB_USERDB_PASSDB_NAME);
        
        
    $db->sql_query("SELECT * FROM projects");
        
        echo 
    "Queries: " $db->query_count
    now, the code runs fine, except that it displays the error when runnign the sql_query function, even tho there is no error?

    can anyone work out why?

    Thanks,
    Justin

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    Specifically, what error is it displaying and you probably need to post your error(..) function code as well.

    If it is displaying a mysql_error() error, that would indicate that the mysql_query() failed and the "or" caused the $this->error(...) to be evaluated.

    You don't seem to be using $this->query, so you probably don't need this line of code - $this->query = $query;

    The three lines of code that are incrementing the counter can be replaced with just - $this->query_count++;
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh, sorry about that. what i mean to say was its calling that error function as in the "or $this->error(mysql_error(), $query);" part, even tho there is no mysql_error(). its just returning an empty error, but displaying it (as that error() function display other hardcoded text as well.)

    its runs as tho there is an error, the statement is fine.

    thanks for the tip in the $this->query_count++.


    Cheers,
    Justin

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    I put your function (less the $this->link to save on typing) into a mini made up class and it works. When the query contains an error (the table name did not exist), the error() function was called and when the query was correct, the error() function was not called -
    PHP Code:
    <?php
    class mysql {
        var 
    $result// the result set
        
    var $query_count;  // just some counter

        
    function sql_query($query) { 
            
    $this->result = @mysql_query($query) or $this->error(mysql_error(), $query); 
            
    $this->query_count++; 
         
        }  

        function 
    error($error$query){
        echo 
    "called the error function<br />";
        echo 
    "Error: $error, Query: $query<br />";
        }
    }
    It is highly likely that your query is failing but your error() function is not displaying the actual information. You would need to post your error() function, as has already been requested, to get the quickest solution to your problem. Either it will contain an error or it will point elsewhere to the problem, but until you post it, it is the most likely reason for your incorrect results.

    Edit: I just thought of another issue. The mysql_query function call might be failing totally, not just because of a query statement problem. Remove the @ sign to see what you get.
    Last edited by CFMaBiSmAd; 06-01-2007 at 01:56 AM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Here is my error function, which is in that same class...

    PHP Code:
    function error($err_msg$query "") {
        
            
    $error_markup "
            <html><head><title>MySQL Error!</title>
            <link rel=\"stylesheet\" href=\"\" /></head>
            <body>
            <div id=\"error_frame\">
            <h1 class=\"h1_err\">MySQL Error!</h1>
            <p class=\"p_err\"><strong>Error Message:</strong></p><br />
            <p class=\"p_err\">"
    .$err_msg."</p><br /><br />
            <p class=\"p_err\"><strong>MySQL Query:</strong></p><br />
            <p class=\"p_err\">"
    .$query."</p><br />
            </div></body></html>
            "
    ;
            
            echo 
    $error_markup;
        

    i will remove the @ sign and see what i get.

    Thanks,

    Justin

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    when ran without the @, i get a

    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource on line 62
    line 62 is

    PHP Code:
    $this->result mysql_query($query$this->link) or $this->error(mysql_error(), $query); 
    here is the part of my class that connects to db (which runs without errors).

    PHP Code:

    class mysql {

        var 
    $query_count;
        var 
    $query;
        var 
    $result;
        var 
    $link;
        
        function 
    mysql($db_host$db_user$db_pass$db_name) {
        
            
    $this->query_count 0;
            
    $this->link 0;
        
            
    $this->connect($db_host$db_user$db_pass$db_name);
        
        }
        
        function 
    connect($db_host$db_user$db_pass$db_name) {
        
            
    $this->link = @mysql_connect($db_host$db_user$db_pass) or $this->error(mysql_error());
            
            
    $this->select_db($db_name);
            
        }
        
        function 
    select_db($db_name) {
            
            
    $this->link = @mysql_select_db($db_name) or $this->error(mysql_error());
            
        }
    //rest of class here, which im not using yet, including the error() function posted above...


    thanks again for your help,

    Justin

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    The most likely problem is the following line in the select_db() function -
    PHP Code:
    $this->link = @mysql_select_db($db_name) or $this->error(mysql_error()); 
    the mysql_select_db() returns a bool true/false. However, you are putting this into the $this->link, which is overwriting any actual resource link.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Regular Coder
    Join Date
    Mar 2006
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    The most likely problem is the following line in the select_db() function -
    PHP Code:
    $this->link = @mysql_select_db($db_name) or $this->error(mysql_error()); 
    the mysql_select_db() returns a bool true/false. However, you are putting this into the $this->link, which is overwriting any actual resource link.


    Hi, thanks for you help... never used link identifiers before, but now im guessing you only set then on the connection, not actually setting the db.

    that worked like a treat!

    thanks heaps,

    Justin

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    but now im guessing you only set then on the connection, not actually setting the db.
    No guessing is required -
    http://www.php.net/manual/en/function.mysql-connect.php
    http://www.php.net/manual/en/functio...-select-db.php
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    lol.

    thanks, it was an "educated" guess. hehe

    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
    •