PHP/MySQL Trying to get property of non-object

So far I have assembled this code to track my drones, I’m getting a Trying to get property of non-object. I cant quite figure out what code modifications I need to make this work, Can someone please explain to me what and where the changes need to be made and why?
[php]

<?php $servername = "localhost"; $username = "somethinghere"; $password = ""; $dbname = "gps"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT devices.positionId, positions.id, devices.`name` as unit, devices.`status` as status, devices.lastUpdate, positions.latitude, positions.longitude, positions.speed, FROM devices, positions WHERE positions.id = devices.id AND devices.`status` = \"online\" "; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["unit"]. " - Name: " . $row["latitude"]. " " . $row["longitude"]. "
"; } } else { echo ""; } $conn->close(); ?> [/php]

Property of what non-object? You just showed some simple MySQLi code, nothing about objects or for that
matter, non-objects. What are you asking? By "properties’ do you mean field types? Or data types?

it throws that error. no results what so ever.

What error? We need the exact error message with all information, not your summary of it.

Anyway, let me guess: Your query has a syntax error (a trailing comma after the last selected comma). Since your code has no error handling whatsoever, it will just keep running with [tt]$result[/tt] being [tt]false[/tt]. When you try to access [tt]$result->num_rows[/tt], the program finally blows up.

To fix this problem, you need to repair the query and also add error handling so that you won’t run into the same situation again. The easiest solution is to enable exceptions for the MySQLi instance:
[php]<?php

$mysqli_driver = new mysqli_driver();
$mysqli_driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;

$conn = new mysqli(…);[/php]
Whenever a query fails, you’ll now get an exception with a detailed description of what went wrong.

query works fine in the command line?

Then you’re running a different query on the command line.

The query above is syntactically invalid, so by definition it cannot work fine. If you copy and paste the exact string content (while replacing the escaped quotes with literal quotes), you’ll get something like this:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM devices, positions WHERE positions.id = devices.id AND devices.`status` ' at line 10

ok im lost now

Lost where? I told you exactly what the problem is and what you need to do to fix it. Do it.

If you’re having trouble with a specific step, then ask a specific question. Merely telling us that you’re lost doesn’t get us anywhere.

I don’t use mysqli_ statements, I use PDO --> So I went to php.net http://php.net/manual/en/mysqli-result.fetch-assoc.php and modify some code. This is what I would do. :wink:
[php]<?php
$mysqli = new mysqli(“localhost”, “my_user”, “my_password”, “gps”);

/* check connection */
if ($mysqli->connect_errno) {
printf(“Connect failed: %s\n”, $mysqli->connect_error);
exit();
}

$query = "SELECT
devices.positionId,
positions.id,
devices.name as unit,
devices.status as status,
devices.lastUpdate,
positions.latitude,
positions.longitude,
positions.speed,
FROM devices, positions
WHERE
positions.id = devices.id AND
devices.status = “online”
";
/* Me thinks this is where you were getting the error */
if ($result = $mysqli->query($query)) {

/* fetch associative array */
while ($row = $result->fetch_assoc()) {
    echo "id: " . $row["unit"]. " - Name: " . $row["latitude"]. " " . $row["longitude"]. "<br>"; 
}

/* free result set */
$result->free();

}

/* close connection */
$mysqli->close();[/php]

Why doesn’t anybody read the replies?

The query is syntactically invalid. As I already said above, there’s a trailing comma:

SELECT ... positions.speed, ^^^ FROM devices, positions ...

As long as the query is wrong, no amount of code will help.

Actually, the code is even worse now, because you’ve suppressed the error message, leaving the OP entirely clueless as to what is wrong. Never suppress errors. They are crucial for recognizing problems and fixing them.

Sponsor our Newsletter | Privacy Policy | Terms of Service