比较从 table 获取的数据时出错

Error while comparing data fetched from the table

我不确定出了什么问题,但它给出了错误消息 - Error1: "Notice: Undefined index: user_type" 和 Error2:"Notice: Undefined index: user_approved",同时存在一个 table userdetails,其中包含名称为 user_type 和 user_approved.

注意:代码可能不完整,但与错误无关,我只是贴出代码供参考,关注的是错误。

 if(isset($_POST['login_btn'])){

                // define variables and set to empty values
                $email = $pwd = "";
                $query1 = $result1 = "";
                $error = FALSE;
                $usertype = $status = "";
                $combo = FALSE;

                $email = validate_input($_POST['user_name']);
                $pwd = validate_input($_POST['user_pwd']);

                include_once("includes/connection.php");

                $query1 = "SELECT * FROM userdetails WHERE user_email='$email' AND user_pwd='$pwd'";

                $result1 = mysqli_query($con,$query1);

                if (!$result1) {
                    echo 'Could not run query: ' . mysql_error();
                    exit;
                }
                else{
                    while($array1= mysqli_fetch_row($result1)){
                        $combo=TRUE;
                        $usertype=$array1['user_type']; **//Error1**
                        $status=$array1['user_approved']; **//Error2**
                    };
                }

如果您想在 $array1 中使用 table 列的名称,您必须使用

$array1 = mysqli_fetch_assoc($result1)

更好用mysqli_fetch_assoc()

例如:

while($array1= mysqli_fetch_assoc($result1)){
   $combo=TRUE;
   $usertype=$array1['user_type']; **//Error1**
   $status=$array1['user_approved']; **//Error2**
};

关于您的问题,请参阅 http://php.net/manual/en/mysqli-result.fetch-row.php

Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). Each subsequent call to this function will return the next row within the result set, or NULL if there are no more rows.

因此它作为索引存储为 0、1、2 等...

while($array1= mysqli_fetch_row($result1)){
   $combo=TRUE;
    $usertype=$array1[0];
    $status=$array1[1];
};

http://php.net/manual/en/mysqli-result.fetch-row.php

mysqli_fetch_row return 结果行作为枚举数组

你可以使用mysqli_fetch_assoc得到

while($array1= mysqli_fetch_assoc($result1)){

select 查询中的确切列而不是 * 然后在下面使用。 $usertype=$array1[0]; $status=$array1[1]; 或者您需要使用这些列的索引,例如如果您是 sql 结果的第 3 列和第 4 列,则使用 $array1[2] 和 $array1[3]