1
$ip = $_SERVER['REMOTE_ADDR'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "images";
$id = $_SESSION['id'];
$ti = null;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM `denied` WHERE ip-adress ='".$ip."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    echo "Sorry " . $row['Name'] . " you have been banned from this site";
}
else {
    echo "<form action='' method='POST'> Enter name to continue: <br/> <input type='text' name='name' placeholder='Enter Name'> <input type='submit' name='continue' value='Continue'> </form> ";
}

For Some reason it returns the error:

Notice: Trying to get property of non-object in C:\xampp\htdocs\Projects\test\test.php on line 218

The line in question is this one:

$result = $conn->query($sql);

but I am positive it has something to do with the line:

$sql = "SELECT * FROM `denied` WHERE ip-adress ='".$ip."'";'
5
  • Please use four spaces for each line of code instead of a > to get the correct syntax highlighting. Commented May 8, 2016 at 21:18
  • I guess you should not use that concatenation '".$ip."'. I think you should use a parametrized query. Take a look here, as it sould not be that hard to figure out. php.net/manual/en/mysqli.prepare.php Commented May 8, 2016 at 21:27
  • Dan's wrong about the error, but right about the principle. Commented May 8, 2016 at 22:22
  • @Strawberry Yeah, I've noticed that the dash is the problem here. Commented May 9, 2016 at 14:06
  • The dash was indeed the problem thanks for the help Commented May 9, 2016 at 15:55

2 Answers 2

4

The problem here is the column name in your WHERE clause:

WHERE ip-adress
        ^ hyphen (a.k.a. minus)

The hyphen in MySQL has special meaning, and it thinks you want to do a subtraction.

I.e.: ip minus address is what it is interpreted as.

You need to wrap that column in ticks

WHERE `ip-adress`

Full line:

$sql = "SELECT * FROM denied WHERE `ip-adress` ='".$ip."'";

Checking for errors against the query http://php.net/manual/en/mysqli.error.php would have thrown you something about it.

Either that, or rename your column to have an underscore instead of a hyphen.

Since you're using sessions, make sure that the session was started.

session_start(); isn't in your posted code.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah changing the collum name fixed thanks for the help
0

The error message in fact says that the object $conn does not exist, you can read that as 'It was not successfully created' if you will.

In any case you could see if the object is looking good by trying: print_r($conn)

Maybe you should try to connect like that instead:

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "SELECT * FROM `denied` WHERE ip-adress ='".$ip."'";
$result = $conn->query($sql);
...

I hope that helps!

2 Comments

It's still gives the same error thanks for the help though
What happens if you try to print_r($conn) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.