比较从表中获取的数据时出错

时间:2015-04-25 07:31:59

标签: php mysql arrays error-handling comparison

我不确定错误是什么,但是它提供了错误消息 - Error1: "Notice: Undefined index: user_type"和错误2:"Notice: Undefined index: user_approved",而存在一个表userdtails,其中包含名为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**
                    };
                }

4 个答案:

答案 0 :(得分:0)

如果要使用$ array1中表格列的名称,则必须使用

$array1 = mysqli_fetch_assoc($result1)

答案 1 :(得分:0)

更好地使用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

  

从结果集中获取一行数据并将其作为一个返回   枚举数组,其中每列存储在数组偏移中   从0(零)开始。每次后续调用此函数都将   返回结果集中的下一行,如果没有则返回NULL   更多行。

所以它作为索引存储为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

答案 2 :(得分:0)

mysqli_fetch_row 将结果行作为枚举数组返回

您可以使用 mysqli_fetch_assoc 来获取

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

答案 3 :(得分:0)

在查询中选择确切的列而不是*,然后在下面使用。 $用户类型= $ ARRAY1 [0];  $状态= $ ARRAY1 [1]; 或者您需要使用这些列的索引,例如如果你的sql结果是第3和第4列,那么使用$ array1 [2]和$ array1 [3]

相关问题