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
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
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: