-1

Good day. I have a little problem over here with my first big project. Iam using alot of SQL statements in my code, all of them work perfectly fine, until this point, were I got an error "Trying to get property of non-object". Xammp tells me, that the error is on line 80.

function btngrp(){
global $connect;
global $listedForums;
global $disableUp;
global $disableDown;
$query = 'SELECT * FROM `categories` ORDER BY `categoryRank` ASC';
$results = $connect -> query($query);
if ($results -> num_rows > 0) {
    while ($row = $results -> fetch_assoc()) {


        echo '<tr class="headlineCat">
            <td colspan="2">' . utf8_encode($row['categoryName']) . '</td>
            <td class="text-right" width="10px">
            <ul class="posBtn">
            <li><button name="categoryOben" class="posUp btnWhite" value="' . $row['categoryID'] . '"><i class="fa fa-caret-up iconPosUp" aria-hidden="true"></i></button></li>
            <li><button name="categoryUnten" class="posDown btnWhite" value="' . $row['categoryID'] . '"><i class="fa fa-caret-down iconPosDown" aria-hidden="true"></i></button></li>
            </ul>
            </td>
            </tr>' . listItemByCategoryID($row['categoryID']) . $listedForums;
                    }
} else {
    echo '<tr><td>Keine Foren gefunden</td></tr>';
}}

I dont really know where my mistake currently is. I've checked, wether my SQL statement is correct. I've executed it in the control pannel and it worked out, therefor, the SQL syntax has to be correct. And as I already've used alot of sql statements, my $connection has to work aswell.

For some reason this function worked out before. But just as I finished my other function to control the Content which is linked with those buttons (positioning and their rank(priority) the error appears

After some "bug fix tries" I noticed, that $results is empty and cant get any content from the query

This is just a small part of the code, the "form" tag and other important tags are just underneath this part :)

So I guess the syntax is actually correct, but is there something like a statement limit in sql? As I've already used around 25 SQL statements or maybe other things iam unaware of?

  • *Trying to get property of non-object* - what's the full error? and which MySQL API are you using to connect with? and you sure you chose the right db and/or table? same for the column(s). – Funk Forty Niner Jul 27 '16 at 19:19
  • Can you var_dump($connect->error) after the query? – Brandon Horsley Jul 27 '16 at 19:20
  • Oh yes, iam sorry if ($results -> num_rows > 0) { thats line 80 I'll give it a try! :D String(52) "Commands out of sync; you can't run this command now" When i use var_dump – Mathias Ivanov Jul 27 '16 at 19:21
  • if you are in fact using the same API to connect with then check for errors on your query http://php.net/manual/en/mysqli.error.php and `$connect` is indeed the right variable. – Funk Forty Niner Jul 27 '16 at 19:21
  • Well, i've inserted this query into my SQL dashboard and it worked out :/ – Mathias Ivanov Jul 27 '16 at 19:24
  • dashboards and PHP are two different animals here. You have a few comments to be answered here. – Funk Forty Niner Jul 27 '16 at 19:24
  • When I just insert SELECT * FROM `categories` ORDER BY `categoryRank` ASC into my SQL database manually it works. Thats why my query has to be correct – Mathias Ivanov Jul 27 '16 at 19:26
  • Your query is fine, the problem is likely that the connection is already servicing a different query, and you need to clean it up before moving on. That is not the fault of this function, per se, but a previous piece of code using that global connection. – Brandon Horsley Jul 27 '16 at 19:57

1 Answers1

0

I suspect this is your problem. You are getting the error about non-object because $results is null or false due to the SQL error.

It may be that a previous query (before your function) has an open cursor. You need to have the previous caller either call $connect->store_result() or modify all calls to query to specify MYSQLI_STORE_RESULT

See the store_result or query docs.

Community
  • 1
  • 1
Brandon Horsley
  • 7,956
  • 1
  • 29
  • 28
  • Thanks for the reply. Sadly, Iam really new to sql and php and dont exactly understand what I have to change – Mathias Ivanov Jul 27 '16 at 19:36
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Jay Blanchard Jul 27 '16 at 19:45
  • Thanks for that, I'll read myself into it and try my best to fix this :) – Mathias Ivanov Jul 27 '16 at 19:55