1

I'm adding a view counter to my website. In the code, I check if there's an IP with the id of the post.

for example when post id is 26, and there's no IP with id 26 in my IP table, it should return 0 but it returns 1 instead.

  $userIp = $_SERVER['REMOTE_ADDR'];
  $checkIp = $db->prepare("SELECT user_ip FROM user_ip WHERE word_id = '$idToShow'");
  $checkIp->execute();
//This happens
  if (count($checkIp) > 0) {
    echo count($checkIp);
    echo " ". $idToShow;
  }
//instead of this
  else {
    $insertIP = $db->prepare("INSERT INTO user_ip (user_ip, word_id) values('$userIp', '$idToShow')");
    $insertIP->execute();
    $updateView = $db->prepare("UPDATE words set views = views + 1 WHERE id = '$idToShow'");
    $updateView->execute();
  }
3
  • what is the value of $idToShow Commented Aug 25, 2018 at 6:44
  • I have a list of words, $idToShow is the id of the word that should be showing. (for example 26)@RAUSHANKUMAR Commented Aug 25, 2018 at 6:47
  • Isn't $checkIp your PDOStatement object or similar? At least it isn't a result set... Commented Aug 25, 2018 at 6:47

6 Answers 6

5

Assuming you are using PDO

Here you are using prepared statements for querying database, but you are not fetching the result that has been returned by a database

use

$result = $checkIp->setFetchMode(PDO::FETCH_ASSOC);
if(count($result) > 0){
  .............
}
else{
  ..........
}

Even simpler you can use

$checkIp->rowCount()

this returns number of rows effected by your query

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

Comments

2

The execute() method runs the query returns a boolean value for if the query successful or not. You can use rowsCount() to count the rows or you can fetchAll() and then count the results.

You should use something like this

$checkIp->execute();
if ($checkIp && $checkIp->rowsCount() > 0) {
    // ...
}

OR

$checkIp->execute();
$ips = $checkIp->fetchAll();
if (count($ips) > 0) {
    // ...
}

http://php.net/manual/en/pdostatement.execute.php
http://php.net/manual/en/pdo.prepare.php

Comments

2

$checkIp->execute(); is always returns boolean value, so your condition is wrong here. check the docs here http://php.net/manual/en/pdostatement.execute.php use like this

$userIp = $_SERVER['REMOTE_ADDR'];
$checkIp = $db->prepare("SELECT user_ip FROM user_ip WHERE word_id = '$idToShow'");
$result=$checkIp->execute();
if (!$result) {
    echo count($checkIp);
    echo " ". $idToShow;
}else {
    $insertIP = $db->prepare("INSERT INTO user_ip (user_ip, word_id) values('$userIp', '$idToShow')");
    $insertIP->execute();
    $updateView = $db->prepare("UPDATE words set views = views + 1 WHERE id = '$idToShow'");
    $updateView->execute();
}

Comments

1

Instead of this

if (count($checkIp) > 0)

Use

if(isset($checkIp->user_ip) && !empty($checkIp->user_ip))

Comments

1

As in PHP doc the execute retunr TRUE or False

$checkIp->execute(); 

so your

(count($checkIp)   

just count the var content that in this case contai just a value

Comments

1

$checkIp is an object and will always (assuming your prepare was successful) return a non-zero count. What you want (assuming you are using mysqli) is $checkIp->num_rows. If you are using PDO, you want $checkIp->rowCount().

Comments

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.