我怎样才能让mysql_result()工作

时间:2018-03-14 12:50:42

标签: mysql

我是编程新手,我一直在尝试为我正在开发的演示网站创建hit_counter,但我一直在为MySQL_result()函数收到错误。

$user_ip = $_SERVER['REMOTE_ADDR'];

function ip_exists($ip) {
  include 'include.php';
  global $user_ip;
  $query = "SELECT `ip` FROM `ip` WHERE`ip`= '$user_ip'";
  $mysql_run = mysqli_query($con, $query);
  $query_num_rows = mysqli_num_rows($mysql_run);
  if ($query_num_rows == 0) {
    return false;
  } else if ($query_num_rows >= 1) {
    return true;
  }
}

function ip_add($ip) {
  include 'include.php';
  $query = "INSERT INTO `ip` VALUES('$ip')";
  $query_run = mysqli_query($con, $query);
}

function update_count() {
  include 'include.php';
  $query = "SELECT `hits` FROM `hit_count`";
  if ($query_run = mysqli_query($con, $query)) {
    $count = mysql_result($query_run, 0, 'hits');
    $count_inc = $count + 1;

    $query_update = "UPDATE ``hit_count` SET `hits`= '$count_inc'";
    $query_update_run = mysqli_query($con, $query_update);
  }
}

if (!ip_exists($user_ip)) {
  update_count();
  ip_add($user_ip);
}

1 个答案:

答案 0 :(得分:0)

您正在混合mysql_*()mysqli_*()个功能;它们属于不同的MySQL扩展,不能互换。由于其余代码使用mysqli请确保您不使用任何mysql_*()函数。旧的MySQL PHP扩展(提供mysql_*()函数)已在PHP 5.5中弃用,并在PHP 7中完全删除。

使用MySQLi,相当于$count = mysql_result($query_run, 0, 'hits')

$row = mysqli_fetch_assoc($query_run);
$count = $row['hits'];