Object of class mysqli_result could not be converted to int

I am making an affiliate program in which I do not want people to be able to mass click the link and gain clicks from the same IP so I made this IP blocker which blocks them after 5 clicks which in theory should work but I get the error above its on line 17 which I will put in bold and underline when I paste my code below

<?php session_start(); if (!isset($_GET['id']) || !is_numeric($_GET['id'])) { die('Invalid id'); } $conn = mysqli_connect("redacted", "redacted", "redacted", "redacted"); if (!$conn) { die("Connection failed: ".mysqli_connect_error()); } $ip = $_SERVER['REMOTE_ADDR']; $sql = ("SELECT ip FROM ips WHERE ip='$ip'"); $qry = $conn->query($sql); if ($qry = 0) { $insert = ("INSERT INTO ips (ip) VALUES ($ip)"); $insertqry = $conn->query($insert); } $sql = ("SELECT times FROM ips WHERE ip='$ip'"); $qry = $conn->query($sql); [u][b]if ($qry < 5) {[/b][/u] $sql = ("UPDATE ips SET times = times+1 WHERE"); $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $id = (int)$_GET['id']; $qry= "UPDATE affiliate SET clicks = clicks+1 WHERE ID='$id'"; $conn->query($qry); $input = array("https://order.abcgameservers.com/aff.php?aff=47", "https://discord.gg/CjzZRBq"); $answer = array_rand($input, 1); //one key header("Location: $input[$answer]"); //use the key to get the array element }elseif ($qry > 5) { $input = array("https://order.abcgameservers.com/aff.php?aff=47", "https://discord.gg/CjzZRBq"); $answer = array_rand($input, 1); //one key header("Location: $input[$answer]"); //use the key to get the array element } ?>

I could not test this because I don’t have a method of call, but running it through the debugger it didn’t show any errors…

I did see that line 11, you had 1 equal sign, that would assign $qry to = 0, if your testing if $qry equals 0, then you need 2 equal signs.

I also changed $qry to <= 4 and >= 5 vs. just testing for < or >. Here is the code:

[php]

<?php session_start(); if (!isset($_GET['id']) || !is_numeric($_GET['id'])) { die('Invalid id'); } $conn = mysqli_connect("redacted", "redacted", "redacted", "redacted"); if (!$conn) { die("Connection failed: ".mysqli_connect_error()); } $ip = $_SERVER['REMOTE_ADDR']; $sql = ("SELECT ip FROM ips WHERE ip='$ip'"); $qry = $conn->query($sql); if ($qry == 0) { $insert = ("INSERT INTO ips (ip) VALUES ($ip)"); $insertqry = $conn->query($insert); } $sql = ("SELECT times FROM ips WHERE ip='$ip'"); $qry = $conn->query($sql); if ($qry <= 4) { $sql = ("UPDATE ips SET times = times+1 WHERE"); $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $id = (int)$_GET['id']; $qry= "UPDATE affiliate SET clicks = clicks+1 WHERE ID='$id'"; $conn->query($qry); $input = array("https://order.abcgameservers.com/aff.php?aff=47", "https://discord.gg/CjzZRBq"); $answer = array_rand($input, 1); //one key header("Location: $input[$answer]"); //use the key to get the array element } elseif ($qry >= 5) { $input = array("https://order.abcgameservers.com/aff.php?aff=47", "https://discord.gg/CjzZRBq"); $answer = array_rand($input, 1); //one key header("Location: $input[$answer]"); //use the key to get the array element // } [/php] Also, I would recommend you do a little more advanced IP detection since that could easily be manipulated... Try something like this: [php] function getIP(){ $bestIP = ''; $bestLocalIP = ''; $ipvars = array('HTTP_X_FORWARDED_FOR','HTTP_NS_CLIENT_IP','HTTP_CLIENT_IP','REMOTE_ADDR'); foreach($ipvars as $ipvar){ $ip = isset($_SERVER[$ipvar]) ? $_SERVER[$ipvar] : null; if($ip && stripos($ip,'unknown')!==0 && strpos($ip,'127.')!==0 && strpos($ip,'10.')!==0 && strpos($ip,'172.16.')!==0 && strpos($ip,'192.168.')!==0 && stripos($ip,'localhost')!==0){ $bestIP = $bestIP=='' ? $ip : $bestIP; } elseif($ip && (strpos($ip,'127.')===0 || strpos($ip,'10.')===0 || strpos($ip,'172.16.')===0 || strpos($ip,'192.168.')===0 || stripos($ip,'localhost')===0)){ $bestLocalIP = $bestLocalIP=='' ? $ip : $bestLocalIP; } } $ip = ($bestIP != '' ? $bestIP : ($bestLocalIP != '' ? $bestLocalIP : 'unknown')); return $ip; } [/php] Then instead of using: $ip = $_SERVER['REMOTE_ADDR']; you could use: $ip = getIP(); and it will get a better result of possible REAL ip addresses.

Well, normally, you open a connection to your database, run a query, fetch the results and then use the results.

You skipped the fetch part! So, loosely and not tested, change your lines like this:
FROM:
[php]
$sql = (“SELECT times FROM ips WHERE ip=’$ip’”);
$qry = $conn->query($sql);
if ($qry < 5) {
[/php]
TO:
[php]
$sql = (“SELECT times FROM ips WHERE ip=’$ip’”);
$qry = $conn->query($sql);
$row = $qry-> result->fetch_assoc());
if ($row[“times”] < 5) {
[/php]
Perhaps you should read this page: https://www.w3schools.com/php/php_mysql_select.asp
It explains the steps and also shows how to add error checking which yours does not have.
You need to fetch the data into an array and then access the array with the name selected. Hope this helps!

Sponsor our Newsletter | Privacy Policy | Terms of Service