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?

    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.

      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.

        Without seeing the error output, we'd just be guessing.

        $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.

          minifairy;11034575 wrote:

          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 ;-)

            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.

              minifairy;11034613 wrote:

              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

              $query = "select * from `inventy` WHERE `shop`='".$shop."' and `qty`>='1' and `cat`!='out' and `imag_url`!='' order by rand() limit 10";

              What is [font=monospace]$shop[/font]? How is it defined? Does it come from the user?

                To debug this, try wrapping the fetch_object() call in a test for [man]is_object/man, and in the event it's found FALSE (not an object), either log or display the SQL statement. Might be worth a shot.

                  6 days later

                  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.

                    Bonesnap;11034871 wrote:

                    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)?

                      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.

                        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?

                          minifairy;11034919 wrote:

                          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:

                          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 ...

                            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

                              minifairy;11035019 wrote:

                              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?

                                With the InnoDB problem, the number of line items in the DB is irrelevant; the issue is that MySQL doesn't release space for deleted records even when the tables have been "optimized" (or whatever it's called for Inno). But, as you mention, the tables aren't InnoDB, so perhaps we're barking up the wrong tree.

                                Have you tried running "myisamchk"?

                                  Yes Trag I copied it exactly from the log

                                    Dalecosp no haven't run myisamchk where do I find it? The system is on a shared host is there a way to run it from phpmyadmin?

                                      There is a "repair table" option in the dropdown at the bottom of the tables list. Select one or more tables and run "with selected > repair table".

                                      #include <disclaimer.h> and all that 😉

                                        Ok did the repair on all tables. Every one came back status OK.