我是编程新手,我一直在尝试为正在开发的演示站点创建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);
}

最佳答案

您正在混合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'];

关于mysql - 我怎样才能使mysql_result()工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49278233/

10-15 17:01