2

I'm getting this error

PHP Notice: Trying to get property of non-object in post.php on line 8

on the code below

<?php
if (is_numeric($_GET["id"])) {
    $mysqli=mysqli_connect("localhost","xxx","xxx","xxx");
    $query = $mysqli->query("SELECT n FROM table WHERE o=".$_GET["id"]);
    if ($query) {
        if ($query->num_rows === 0) {
                header("HTTP/1.1 301 Moved Permanently"); 
                header("Location: http://www.example.com/?p=".$query->fetch_object()->n);
        }
    }
    mysqli_close($mysqli);
}
else {
    echo $_GET["id"];

}
?>

Any idea what's happening? I presumed the checks I had in place would ensure only valid data was being passed

Thanks

Saty
  • 22,213
  • 7
  • 30
  • 49
pee2pee
  • 3,445
  • 6
  • 48
  • 120

2 Answers2

2

table is reserved keyword in mysql it must be in backtict. Your query fails to give result that's the reason you are getting this error

SELECT n FROM `table` WHERE o=".$_GET["id"]

UPDATED

for fetching data you have to use while loop

while ($obj = $query->fetch_object()) {
            header("Location: http://www.example.com/?p=".$obj->n);
    }

Check http://php.net/manual/en/mysqli-result.fetch-object.php

Saty
  • 22,213
  • 7
  • 30
  • 49
0

you are trying to handle an empty result. you ask if the result has 0 rows, then try to fetch a row tho the result must be 0 rows:

if ($query->num_rows === 0) { // num_rows === 0
    header("HTTP/1.1 301 Moved Permanently");         //fetch with 0 rows?
    header("Location: http://www.example.com/?p=".$query->fetch_object()->n);
}
Tanuel Mategi
  • 1,251
  • 6
  • 13