more mysqli issues
Page 1 of 2 12 LastLast
Results 1 to 15 of 27

Thread: more mysqli issues

  1. #1
    Member
    Join Date
    Dec 2007
    Posts
    71

    Unhappy more mysqli issues

    Ok I've moved all the $mysqli->query($query) statements out to a function and also have a variant that returns $result with the $result = $mysqli->query($query); also in the function.

    I'm doing queries on databases that I know have results but I keep getting errors in my error log.
    PHP Fatal error: Call to a member function fetch_object() on a non-object in ...
    However, I cannot get the program to die or reproduce that error either on my local system or online when I access it. I can circumvent the error by enclosing all the code in an
    if($result)
    statement but that sort of eliminates all the stuff that is going to be displayed and I know that the results should be coming out of the database as I'm not asking for something that wouldn't be there.

    Any ideas?

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,730
    The result object is not an object because the query failed.

    When querying a MySQL server, always test the query. Use the appropriate foo_error() function to find out why the query is returning empty --- that's the only way.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  3. #3
    Member
    Join Date
    Dec 2007
    Posts
    71
    But the foo_error thing isn't going to end up in the error log is it? I understand that the error is saying the query failed, but the query shouldn't have failed and I can't get it to fail for me. Everytime I access the program it works as expected and I don't get errors in the error log. But I'm finding errors in the error log pretty frequently. I don't know how to fix what I can't reproduce.

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,986
    Without seeing the error output, we'd just be guessing.
    PHP Code:
    $result $mysqli->query($query);
    if(
    $result == false) {
       
    error_log("Query failed:\n".$mysqli->error."\n$query");
       die(
    "Sorry, there was an unexpected DB error. Debug information has been logged.");

    Now if we see the output of that error_log(), we might be able to figure out what the issue is.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,730
    Quote Originally Posted by minifairy View Post
    But the foo_error thing isn't going to end up in the error log is it? I understand that the error is saying the query failed, but the query shouldn't have failed and I can't get it to fail for me. Everytime I access the program it works as expected and I don't get errors in the error log. But I'm finding errors in the error log pretty frequently. I don't know how to fix what I can't reproduce.
    Good point. Do as NogDog suggests and write a custom logging function that writes to file or DB. That should help a lot.

    Just for curiosity's sake, any possibility that the application isn't sanitizing the data before attempting to write to the DB? Those apostrophe edge cases have bitten more than one programmer in the past ;-)
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  6. #6
    Member
    Join Date
    Dec 2007
    Posts
    71
    Thanks NogDog I'll try that and see what I get. The only error output I am gettting at the moment is
    PHP Fatal error: Call to a member function fetch_object() on a non-object in ... program xxx.php at line xxx
    Its very odd to me because it is not a public database and I know what is in the database, so no apostrophes. I'm probably not the best at sanitizing data but I'm pretty sure its good and I know that it shouldn't be failing. Furthermore I can not get it to fail myself. One of the routines that seems to fail most often is
    $query = "select * from `inventy` WHERE `shop`='".$shop."' and `qty`>='1' and `cat`!='out' and `imag_url`!='' order by rand() limit 10";
    I know what I put into the inventory, I know the shop is set on entry to the program I know there are at least 100 items that should satisfy the criteria and everytime I load the program it works fine no errors. So it just kinda has me scratching my head.

  7. #7
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by minifairy View Post
    I'm probably not the best at sanitizing data but I'm pretty sure its good and I know that it shouldn't be failing ... One of the routines that seems to fail most often is
    PHP Code:
    $query "select * from `inventy` WHERE `shop`='".$shop."' and `qty`>='1' and `cat`!='out' and `imag_url`!='' order by rand() limit 10"
    What is $shop? How is it defined? Does it come from the user?
    Last edited by traq; 10-24-2013 at 03:01 PM.

  8. #8
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,730
    To debug this, try wrapping the fetch_object() call in a test for is_object(), and in the event it's found FALSE (not an object), either log or display the SQL statement. Might be worth a shot.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  9. #9
    Senior Member
    Join Date
    Mar 2009
    Posts
    820
    Something else I have encountered in the past, when I am sure of my input and the data that's in the database, yet the query is always failing: check the spelling of your columns and tables. I wish I could say I'd be surprised how many times that has tripped me up. Now it's something I check for regularly.
    Declare variables, not war.

  10. #10
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    Quote Originally Posted by Bonesnap View Post
    Now it's something I check for regularly.
    Doesn't your DBMS already do that for you (e.g. via the use of "Unknown identifier 'usernaem' on line 4" error messages)?

  11. #11
    Member
    Join Date
    Dec 2007
    Posts
    71
    Ok if it were failing all the time I would look for mispellings, however, its not. In fact I can't get it to fail for me I just find these errors in the error log later. I put in the log bit that NOGDOG suggested and I now am getting
    Got error -1 from storage engine
    along with the query which looks fine and is working every time I try it. In fact I can copy the query that is logged and run it in phpmyadmin and it works fine. I've searched all over and can not find any reference to what the heck a -1 error from the storage engine is. Can anyone enlighten me?

    Thanks.

  12. #12
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Is that - 1 (dash, one) or -1 (negative one)?

    MySQL error codes (AFAIK) start at 1000. If that's (not negative) one, then it might be a system error mysql is encountering. "1" is "Operation not permitted" for most linux-based OS's...

    = = = = = edit = = = = =

    Can you show us the actual code you're using to generate your error log?
    Last edited by traq; 10-31-2013 at 02:18 PM.

  13. #13
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,730
    Quote Originally Posted by minifairy View Post
    I've searched all over and can not find any reference to what the heck a -1 error from the storage engine is. Can anyone enlighten me?
    A common cause is no disk space for InnoDB tables. Check that. If this is a production system you're probably going to have some fun until you can get this taken care of.

    You may want to change MySQL's datadir.

    You may want to set:
    Code:
    innodb_file_per_table = 1
    in your "my.cnf" file.

    You may even need to dump and re-create some databases.

    I guess we can hope it's something else ...
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  14. #14
    Member
    Join Date
    Dec 2007
    Posts
    71
    Trag I have no idea if it is a dash 1 or a -1 and I don't know how I could tell the difference. I copied the error directly from the log file. The function that is logging the error is:
    function querymeres($query){
    #query with result
    global $mysqli;
    #include_once('./bits/show-connect.inc.php');
    $result = $mysqli->query($query);
    if($result == false) {
    error_log("Query failed:\n".$mysqli->error."\n$query");
    }else{
    return $result;
    }
    }
    One of the queries that produced such an error is:
    select * from `inventy` WHERE `shop`='1' and `qty`>='1' and `cat`!='out' and `imag_url`!='' order by rand() limit 10
    I happen to know that there are probably at least 100 items that satisfy the criteria in the table.

    Dalecosp All of the tables in the database are MyIsam tables so I don't think some InnoDB issue would be the cause.

    I'm supposed to have unlimited space and my databases are not ginormous. A couple of them I would call pretty large but none of my tables have as many as 7 or 8 thousand entries and most have less than 1000

  15. #15
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by minifairy View Post
    error_log("Query failed:\n".$mysqli->error."\n$query");
    Doesn't look like a dash, then… was "Got error -1 from storage engine" the exact text of the error message?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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