-2

I have a website which logs the ip,time and date of every visit. I am trying to write a function that displays how many times a certain ip address has accessed the site. It will say something like you have visited this site .... times.

Here is the php:

<?php 
function visitor_counter()
{

    require_once 'php_scripts/log_in_script_2.php';
    $conn = new mysqli($hn,$un,$pw,$db);
    if ($conn->connect_error) die($conn->connect_error);

    $ip_var = $_SERVER['REMOTE_ADDR'];

    $query = "SELECT COUNT(ip) FROM visitors WHERE ip = '$ip_var'";

    $result = $conn1->query($query);

    if (!$result) die ($conn->error);

    echo $result;

    mysqli_close();

}
?>

it is included in the webpage with

<?php require_once "php_scripts/visit_counter.php"; ?>

and the function is called in the code with

You have visited my site: <?php visitor_counter(); ?> times.

It does not output anything, and the rest of the webpage doesn't load after this line. I know the query is correct, I've tested it in phpmyadmin, and I know the login for the function has the correct privilages. Any ideas?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
joshpj1
  • 186
  • 1
  • 1
  • 9
  • 1
  • If the rest of the page doesn't load, then one of the `die()` cases is in play. Try to `echo` the error, rather than `die()` it. – Derenir Mar 16 '16 at 15:11
  • And this line `$result = $conn1->query($query);` shouldn't be like `$result = $conn->query($query);` ? I can't see any $conn1 in your code. – Derenir Mar 16 '16 at 15:16
  • 2
    http://php.net/manual/en/function.error-reporting.php would have signaled `undefined variable conn1`... – Funk Forty Niner Mar 16 '16 at 15:18
  • @Fred-ii- that could help too. :) – Derenir Mar 16 '16 at 15:19
  • @Derenir *Yep!* Good catch on that "undefined variable" ;-) That was staring me in the face the whole time *lol* – Funk Forty Niner Mar 16 '16 at 15:20
  • 1
    possible duplicate of [PHP: Notice: Undefined variable and Notice: Undefined index](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Funk Forty Niner Mar 16 '16 at 15:20
  • `$result` is not what you think, it is in fact a mysql_result. _From the manual: Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object._ [**So read the manual**](http://php.net/manual/en/mysqli.query.php) Then run something like `mysqli_fetch_assoc ( $result )` to actually unload a row from the result set, [The Manual again](http://php.net/manual/en/mysqli-result.fetch-assoc.php) I think what I am really saying here is __Read The Manual__ – RiggsFolly Mar 16 '16 at 16:44
  • 1
    Oh and @Fred-ii- 's first comment is also very relevant. Again, there is no substitute for __reading the manual__ – RiggsFolly Mar 16 '16 at 16:51

1 Answers1

1

I added the changes from the comments above, but a problem was still there. The problem was that I was trying to echo the query result directly. I changed the query to $query = "select count(ip) as total_visits from visitors where ip = '$ip_var'"; and changed the echo to echo $result->fetch_object()->total_visits;, and it works.

joshpj1
  • 186
  • 1
  • 1
  • 9
  • I'm always found of someone, who can answer his own question, from the help "we" (as SO commenters) provide. Also, I really don't appreciate, when a post is voted down, just because the OP isn't asked the question in the right way. Especially, when it's a newcomer's question. 'Cause of that, I'm gonna vote you up, and hope, you don't loose hope or trust in the platform. :) – Derenir Mar 19 '16 at 00:08