GoddamnAllien
GoddamnAllien

Reputation: 70

PHP: getting 1 instead of 0 using count()

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();
  }

Upvotes: 1

Views: 960

Answers (6)

David Lartey
David Lartey

Reputation: 587

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

Upvotes: 2

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

Upvotes: 5

RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6006

$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();
}

Upvotes: 2

Nick
Nick

Reputation: 147166

$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().

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133360

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

Upvotes: 1

LahiruTM
LahiruTM

Reputation: 648

Instead of this

if (count($checkIp) > 0)

Use

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

Upvotes: 1

Related Questions