Database hit counter issue

Discussion in 'PHP' started by sash_007, Oct 24, 2019.

  1. #1
    Hello friends,

    I am new to php

    and have been following a tutorial to build a php mysql hit counter
    my code for connect.inc.php
    
    
    <?php
    $conn_error = "could not connect";
    $mysql_host= "localhost";
    $mysql_user = "root";
    $mysql_pass ="";
    $mysql_db ="a_database";
    
    $conn = mysqli_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_db);
    
    /*if(!mysqli_connect($mysql_host,$mysql_user,$mysql_pass) && !mysqli_select_db($mysql_db)){
        die($conn_error);
    }
    */
    if(!$conn){
         die("Connection failed: ". mysqli_connect_error());
       
    }else{
         echo "ok";
    }
    
    ?>
    
    PHP:

    code for index.php

    
    <?php
    
    require "connect.inc.php";
    $user_ip = $_SERVER["REMOTE_ADDR"];
    function ip_exists($ip){
              global $user_ip;
           
          
    }
    function update_count(){
         $query = "SELECT `count`  FROM `hits_count`";
       
         if(@$query_run = mysqli_query($query)){
             $count = mysql_result($query_run,0,"count");
             echo $count;
         }
       
    }
    update_count();
    
    ?>
    PHP:
    when i just echo count variable it shows nothing but blank screen
    but database connection is working fine as i see ok printed on screen

    what's wrong here?

    i am using wamp server to test these codes
     
    sash_007, Oct 24, 2019 IP
  2. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #2
    This part here:
    $count = mysql_result($query_run,0,"count");
    PHP:
    should it not be:
    $count = mysqli_result($query_run,0,"count");
    PHP:
    Note mysqli not mysql
     
    malky66, Oct 25, 2019 IP
  3. sash_007

    sash_007 Well-Known Member

    Messages:
    174
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    125
    #3
    Nope this code didnt work
    
    function update_count(){
         $query = "SELECT `count`  FROM `hits_count`";
      
         if(@$query_run = mysqli_query($query)){
             $count = mysqli_result($query_run,0,"count");
             echo $count;
         }
      
    }
    PHP:
    i cleared my browser cache but still no luck

    ok this code worked
     function update_count(){
         global $conn;
         $query = "SELECT `count`  FROM `hits_count`";
        
         if($query_run = mysqli_query($conn,$query)){
             //$count = mysqli_result($query_run,0,"count");
             $row = mysqli_fetch_assoc($query_run);
             $count = $row["count"];
             echo $count;
         }
        
    }
    PHP:
     
    Last edited: Oct 25, 2019
    sash_007, Oct 25, 2019 IP