php 警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19232152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:04:16  来源:igfitidea点击:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in

phpmysqli

提问by Sibel Sar?kaya

I tried to change my mysql to mysqli. And when i do this i get error. I tried everything but i found no solution for this.

我试图将我的 mysql 更改为 mysqli。当我这样做时,我会出错。我尝试了一切,但我没有找到解决方案。

The error that I have: (what you see is not all the errors)

我的错误:(你看到的不是所有的错误)

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
C:\xampp\htdocs\follow\include\database.php on line 219

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\follow\include\database.php on line 220

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\follow\include\database.php on line 231

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\follow\include\database.php on line 232

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\follow\include\database.php on line 102

... more errors

警告:mysqli_query() 期望参数 1 为 mysqli,
C:\xampp\htdocs\follow\include\database.php 中第 219 行给出的空值

警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,在 C:\xampp\htdocs\follow\include\database.php 第 220 行中给出为空

警告:mysqli_query() 期望参数 1 为 mysqli,C:\xampp\htdocs\follow\include\database.php 中第 231 行给出的空值

警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,空值在 C:\xampp\htdocs\follow\include\database.php 第 232 行

警告:mysqli_query() 期望参数 1 为 mysqli,C:\xampp\htdocs\follow\include\database.php 中第 102 行给出的 null

...更多错误

and this is my php code

这是我的 php 代码

<?php
/**
 * Please subscribe to our feeds at http://blog.geotitles.com for more such tutorials
 */
include("constants.php");
class MySQLDB
  {
  var $connection;         //The MySQL database connection
 var $num_active_users;   //Number of active users viewing site
 var $num_active_guests;  //Number of active guests viewing site
 var $num_members;        //Number of signed-up users
 /* Note: call getNumMembers() to access $num_members! */

   /* Class constructor */
  function MySQLDB(){
  /* Make connection to database */

 $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS,DB_NAME);

  // Check connection
 if (mysqli_connect_errno())
     {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }



    /** 
    * Only query database to find out number of members
    * when getNumMembers() is called for the first time,
    * until then, default value set.
    */
    $this->num_members = -1;

    if(TRACK_VISITORS){
     /* Calculate number of users at site */
     $this->calcNumActiveUsers();

     /* Calculate number of guests at site */
     $this->calcNumActiveGuests();
    }
   }


   function confirmUserPass($username, $password){
   GLOBAL $con;
   /* Add slashes if necessary (for query) */
  if(!get_magic_quotes_gpc()) {
      $username = addslashes($username);
   }

   /* Verify that user is in database */
   $q = "SELECT password FROM ".TBL_USERS." WHERE username = '$username'";
   $result = mysqli_query($con,$q);
   if(!$result || (mysqli_num_rows($result) < 1)){
     return 1; //Indicates username failure
   }

   /* Retrieve password from result, strip slashes */
   $dbarray = mysqli_fetch_array($result);
   $dbarray['password'] = stripslashes($dbarray['password']);
   $password = stripslashes($password);

  /* Validate that password is correct */
  if($password == $dbarray['password']){
     return 0; //Success! Username and password confirmed
   }
   else{
     return 2; //Indicates password failure
  }
  } 


   function confirmUserID($username, $userid){
   GLOBAL $con;
  /* Add slashes if necessary (for query) */
  if(!get_magic_quotes_gpc()) {
      $username = addslashes($username);
  }

  /* Verify that user is in database */
  $q = "SELECT userid FROM ".TBL_USERS." WHERE username = '$username'";
  $result = mysqli_query($con,$q);
  if(!$result || (mysqli_num_rows($result) < 1)){
     return 1; //Indicates username failure
   }

  /* Retrieve userid from result, strip slashes */
  $dbarray = mysqli_fetch_array($result);
  $dbarray['userid'] = stripslashes($dbarray['userid']);
  $userid = stripslashes($userid);

  /* Validate that userid is correct */
  if($userid == $dbarray['userid']){
     return 0; //Success! Username and userid confirmed
  }
  else{
     return 2; //Indicates userid invalid
  }
  }


  function usernameTaken($username){
   GLOBAL $con;
    if(!get_magic_quotes_gpc()){
     $username = addslashes($username);
   }
  $q = "SELECT username FROM ".TBL_USERS." WHERE username = '$username'";
  $result = mysqli_query($con,$q);
  return (mysqli_numrows($result) > 0);
  }


 function usernameBanned($username){ 
 GLOBAL $con;
  if(!get_magic_quotes_gpc()){
     $username = addslashes($username);
  }
  $q = "SELECT username FROM ".TBL_BANNED_USERS." WHERE username = '$username'";
  $result = mysqli_query($con,$q);
  return (mysqli_numrows($result) > 0);
  }

function addNewUser($username, $password, $email){
GLOBAL $con;
  $time = time();
  /* If admin sign up, give admin user level */
  if(strcasecmp($username, ADMIN_NAME) == 0){
     $ulevel = ADMIN_LEVEL;
  }else{
     $ulevel = USER_LEVEL;
  }
  $q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password', '0', $ulevel,     '$email', $time)";
  return mysqli_query($con,$q);
  }


 function updateUserField($username, $field, $value){
  $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
  return mysqli_query($con,$q);
}


   function getUserInfo($username){
 GLOBAL $con;
  $q = "SELECT * FROM ".TBL_USERS." WHERE username = '$username'";
  $result = mysqli_query($con,$q);
  /* Error occurred, return given name by default */
  if(!$result || (mysqli_num_rows($result) < 1)){
     return NULL;
  }
  /* Return result array */
  $dbarray = mysqli_fetch_array($result);
  return $dbarray;
  }


 function getNumMembers(){
 GLOBAL $con;
  if($this->num_members < 0){
     $q = "SELECT * FROM ".TBL_USERS;
     $result = mysqli_query($con,$q);
     $this->num_members = mysqli_num_rows($result);
   }
  return $this->num_members;
  }

    /**
   * calcNumActiveUsers - Finds out how many active users
   * are viewing site and sets class variable accordingly.
   */
   function calcNumActiveUsers(){
 GLOBAL $con;
  /* Calculate number of users at site */
  $result = mysqli_query($con,"SELECT * FROM ".TBL_ACTIVE_USERS);
  $this->num_active_users = mysqli_num_rows($result);
     }


  function calcNumActiveGuests(){  
  GLOBAL $con;
  /* Calculate number of guests at site */
  $q = "SELECT * FROM ".TBL_ACTIVE_GUESTS;
  $result = mysqli_query($con,$q);
  $this->num_active_guests = mysqli_num_rows($result);
   }


   function addActiveUser($username, $time){
 GLOBAL $con;
  $q = "UPDATE ".TBL_USERS." SET timestamp = '$time' WHERE username = '$username'";
  mysqli_query($con,$q);

  if(!TRACK_VISITORS) return;
  $q = "REPLACE INTO ".TBL_ACTIVE_USERS." VALUES ('$username', '$time')";
  mysqli_query($con,$q);
  $this->calcNumActiveUsers();
   }

 function addActiveGuest($ip, $time){
 GLOBAL $con;
  if(!TRACK_VISITORS) return;
  $q = "REPLACE INTO ".TBL_ACTIVE_GUESTS." VALUES ('$ip', '$time')";
  mysqli_query($con,$q);
  $this->calcNumActiveGuests();
   }

 function removeActiveUser($username){
 GLOBAL $con;
  if(!TRACK_VISITORS) return;
  $q = "DELETE FROM ".TBL_ACTIVE_USERS." WHERE username = '$username'";
  mysqli_query($con,$q);
  $this->calcNumActiveUsers();
   }

/* removeActiveGuest */
function removeActiveGuest($ip){
GLOBAL $con;
  if(!TRACK_VISITORS) return;
  $q = "DELETE FROM ".TBL_ACTIVE_GUESTS." WHERE ip = '$ip'";
  mysqli_query($con,$q);
  $this->calcNumActiveGuests();
}

/* removeInactiveUsers */
function removeInactiveUsers(){
GLOBAL $con;
  if(!TRACK_VISITORS) return;
  $timeout = time()-USER_TIMEOUT*60;
  $q = "DELETE FROM ".TBL_ACTIVE_USERS." WHERE timestamp < $timeout";
  mysqli_query($con,$q);
  $this->calcNumActiveUsers();
}

/* removeInactiveGuests */
function removeInactiveGuests(){
 GLOBAL $con;
  if(!TRACK_VISITORS) return;
  $timeout = time()-GUEST_TIMEOUT*60;
  $q = "DELETE FROM ".TBL_ACTIVE_GUESTS." WHERE timestamp < $timeout";
  mysqli_query($con,$q);
  $this->calcNumActiveGuests();
}


function query($query){
GLOBAL $con;
  return mysqli_query($con,$query);
}
};


  $database = new MySQLDB;

  ?>

回答by newfurniturey

The error is fairly vague, but specific. It's stating that a connection object, mysql_result, is expected but it's receiving nullinstead.

该错误相当模糊,但具体。它表示需要一个连接对象,mysql_result但它正在接收null

The reason for this is how you're attempting to use the connection inside each function:

这样做的原因是您尝试在每个函数中使用连接的方式:

global $con;
mysqli_query($con, $q);

The globalkeyword willbring in global variables to the function you're using it in, but $conisn't a global variable in this class (and, you don't need to use globalinside classes). What you're looking for instead is the $thiskeyword.

global关键字会将全局变量引入您正在使用它的函数中,但$con不是此类中的全局变量(并且,您不需要global在类内部使用)。您正在寻找的是$this关键字。

In your class constructor MySQLDB()you define the $convariable; you should instead change this to use $this->connectionas you've already defined a global variable named $connection:

在您的类构造函数中,MySQLDB()您定义了$con变量;您应该改为使用它,$this->connection因为您已经定义了一个名为 的全局变量$connection

$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS,DB_NAME);

Then, in each function, instead of using something like:

然后,在每个函数中,而不是使用类似的东西:

global $con;
mysqli_query($con,$q);

You can use:

您可以使用:

mysqli_query($this->connection, $q);

回答by Niet the Dark Absol

You define $conin your constructor (which you should be storing in $this->connectionsince you prepared a variable for it). Because it's defined in a function, it is not a global variable, and therefore cannot be imported with global $con;. If you use $this->connectionproperly everywhere you use the connection handler, then it should work.

$con在构造函数中定义($this->connection因为您为它准备了一个变量,所以您应该将其存储在其中)。因为它是在函数中定义的,所以它不是全局变量,因此不能用global $con;. 如果在使用$this->connection连接处理程序的任何地方都正确使用,那么它应该可以工作。

回答by Israel Temitope

Just manage the user privileges by adding the user to the database. It will solve the problem

只需通过将用户添加到数据库来管理用户权限。它会解决问题