-2

Ok so I've tried multiple times to get this to work but I just don't know what it means by this "Trying to get property of non-object in" has anyone got any ideas on how to fix this I'll paste the code down below I know I'm using mysql and not mysql so please don't comment on that I'm not too bothered about security atm I just want to get it to work thanks guys.

<?php

    $servername = "localhost";
    $username = "godhacks";
    $password = "Gijriom71";
    $dbname = "godhacks_nukleusproject";


    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT id, game-name, game-load-url, img-url, game-status FROM loaderpc ORDER BY id ASC LIMIT 9,999";
    $result = $conn->query($sql);



    if ($result->num_rows > 0) {

        // output data of each row
        while($row = $result->fetch_assoc()) {

            echo "<div class='col-3'>" ;
            echo "<div class='game-card clearfix'>";
            echo "<a href=".$row["game-load-url"]." style='background-image: url(".$row["img-url"].");'>"  ;
            echo "<div class='card-inner'>";
            echo "<div class='game-status'>" .$row["game-status"]. "</div>"   ;

            echo "<div class='game-overlay'>";
            echo "<div class='game-details'>";
            echo "<span class='game-name'>".$row["game-name"]."</span>";
            echo "<div class='game-load'>click to load</div>";
            echo "</div>";
            echo "<div class='blur-box' style='background-image: url(".$row["img-url"].");'></div>";
            echo "</div>";
            echo "</div>";
            echo "</a>";
            echo "</div>";
            echo "</div>";







        }

    } else {
        echo "0 results";
    }


    $conn->close();
    ?>
Scath
  • 3,673
  • 10
  • 27
  • 36
Kyle Bown
  • 21
  • 7
  • Which line is the error on? Can you please provide the full error? – Reisclef Aug 04 '17 at 19:23
  • 1
    Take the comma out of your `LIMIT 9,999`. It's causing your SELECT query to fail. – aynber Aug 04 '17 at 19:24
  • 125 " if ($result->num_rows > 0) {" – Kyle Bown Aug 04 '17 at 19:24
  • It means you're doing object related operations on something that's not an object. Just read the error a few times until it clicks. The `$result` is obviously NOT an object. Now, if it's not an object, that means something went terribly wrong and you never checked whether the bad thing happens. Hopefully you learned a valuable lesson. – N.B. Aug 04 '17 at 19:25
  • @N.B., That doesn't help me in anyway – Kyle Bown Aug 04 '17 at 19:26
  • @aynber, I've done that but its still failing – Kyle Bown Aug 04 '17 at 19:27
  • 1
    Advice about doing basic error checking doesn't help you? Oh boy, are you going to have fun in this business then :) – N.B. Aug 04 '17 at 19:27
  • 1
    To reword what N.B. said, check for [mysqli_errors](http://php.net/manual/en/mysqli.error.php) after you run your query and before you attempt to use the results. That will tell you what's wrong if the query fails. – aynber Aug 04 '17 at 19:27
  • @aynber why would this cause the query to fail? `LIMIT 9,999` is perfectly valid imho. – masterfloda Aug 04 '17 at 19:28
  • 1
    The columns contain a `-` sign. MySQL will try to perform arithmetic operations. The columns should be quoted with a backtick. Doing a simple check whether query succeeded and would uncover that. The part that's bad is that OP would have discovered this on his own, and earned a metric ton of experience while doing so. – N.B. Aug 04 '17 at 19:29
  • 1
    True, `LIMIT 9,999` will return the rows from 9-999. I forgot about that. However, looking at the query, the columns will need to be surrounded by backticks because of the hyphens. https://stackoverflow.com/questions/3168644/can-a-table-field-contain-a-hyphen – aynber Aug 04 '17 at 19:29
  • 1
    its not a mysql error its a php error here's the full error message i get `[04-Aug-2017 19:24:57 UTC] PHP Notice: Trying to get property of non-object in /home/godhacks/public_html/nukleus/loader/loaderPC/pcgames.php on line 125` – Kyle Bown Aug 04 '17 at 19:30
  • Kyle, your `$result` is boolean `false`. That's because `$conn->query()` didn't succeed, and it didn't succeed because you provided it with incorrect SQL. Boolean `false` is not an object. You're trying to do `false->num_rows`, and you can't. That's why you get `trying to get a property on a non-object`. Quote your column names with a backtick (`) or just don't use a minus sign for column name. – N.B. Aug 04 '17 at 19:31

2 Answers2

0

Ok i've fixed it thanks to @N.B here's the fixed version

<?php

    $servername = "localhost";
    $username = "godhacks";
    $password = "Gijriom71";
    $dbname = "godhacks_nukleusproject";


    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT id, game_name, game_load_url, img_url, game_status FROM loaderpc ORDER BY id ASC LIMIT 9999";
    $result = $conn->query($sql);



    if ($result->num_rows > 0) {


        while($row = $result->fetch_assoc()) {

            echo "<div class='col-3'>" ;
            echo "<div class='game-card clearfix'>";
            echo "<a href=".$row["game_load_url"]." style='background-image: url(".$row["img_url"].");'>"  ;
            echo "<div class='card-inner'>";
            echo "<div class='game-status'>" .$row["game_status"]. "</div>"   ;

            echo "<div class='game-overlay'>";
            echo "<div class='game-details'>";
            echo "<span class='game-name'>".$row["game_name"]."</span>";
            echo "<div class='game-load'>click to load</div>";
            echo "</div>";
            echo "<div class='blur-box' style='background-image: url(".$row["img_url"].");'></div>";
            echo "</div>";
            echo "</div>";
            echo "</a>";
            echo "</div>";
            echo "</div>";







        }

    } else {
        echo "0 results";
    }


    $conn->close();
    ?>
Kyle Bown
  • 21
  • 7
  • Never post your passwords. I suggest you change this password on your system, even if it's for local testing or playing around. This password is probably now in hacking dictionaries. – N.B. Aug 04 '17 at 19:40
0

This SQL is incorrect:

$sql = "SELECT id, game-name, game-load-url, img-url, game-status FROM loaderpc ORDER BY id ASC LIMIT 9,999";

It's incorrect because of - sign in column names. This is why you're advised to use alphanumeric characters for column names, or if you're using reserved characters or words - to quote the columns with a backtick.

What happens is that your query fails and $result contains boolean value false. You're trying to perform false->num_rows and you get the warning about accessing a property on a non-object, as false isn't an object.

To avoid these issues in the future, check whether your queries succeed with:

if(!$result)
{
    echo 'An error occurred: '. $conn->error;
    exit;
}
N.B.
  • 12,505
  • 3
  • 38
  • 50