0

在下面的代码中,我将从数据库中提取的 6 个值分配给数组 'arrayRetrievedUserInfo' ;但如果我打印这个数组,它只显示其中两个。接下来,我将此数组分配给另一个数组 'arrayResponse' ;此数组包含 arrayRetrievedUserInfo 和其他两个字段。但是如果我打印这个数组,它会显示 arrayRetrievedUserInfo 的四个值,这些值在直接打印 arrayRetrievedUserInfo 时没有显示。请帮我解决这个问题。

public function signIn()
    {
        //code for signing in the user
        try
        {
            $success=0;
            //query to retrieve user info
            $query_sign_in="SELECT * FROM users WHERE user_name='".$this->userName."'and user_pass='".$this->password."'";
            $result_sign_in=mysql_query($query_sign_in,$this->db)or die (mysql_error());
            if($result_sign_in!=0)
            {
                /*
                Check if record exist against this username-password combination
                */
                if(mysql_num_rows($result_sign_in)==0)
                {
                    //Username-password combination does not exit
                    $arrayErrors['no_user']=$this->USERNAME_PASSWORD_COMBINATION_ERROR;
                    $success=0;
                }   
                else
                {
                    //Username-password combination exist
                    while($row=mysql_fetch_array($result_sign_in))
                    {
                        //Retrieve the user's data from database
                        $arrayRetrievedUserinfo['user_id']=$row["user_id"];
                        $arrayRetrievedUserInfo['first_name']=$row["first_name"];
                        $arrayRetrievedUserInfo['last_name']=$row["last_name"];
                        $arrayRetrievedUserinfo['username']=$row["user_name"];
                        $arrayRetrievedUserInfo['email']=$row["user_email"];
                        $arrayRetrievedUserInfo['user_type']=$row["user_level"];    //1-admin 0->non-admin  

                        echo "Echoing database elements individually";
                        echo $row['first_name']."<br />";
                        echo $row['last_name']."<br />";
                        echo $row['user_name']."<br />";
                        echo $row['user_email']."<br />";
                        echo $row['user_id']."<br />";
                        echo "-------------------------------<br/>";

                    }           
                    $success=1; 

                        echo "Printing the contents of the arrayRetrievedUserinfo using a for loop  <br/>";
                        foreach ($arrayRetrievedUserinfo as $key) 
                        {
                            echo $key."\n";
                            echo "\n";
                        }
                        echo "<br>--------------------------<br>";                  

                }
            }
            else
            {
                $arrayErrors['db']=$this->DATABASE_CONNECTION_ERROR;
                $success=0;
            }


                            if($success)
                            {
                                    //Query executed successfully
                                    $arrayResponse["success"]=1;                            //staus of the operation
                                    $arrayResponse["errors"]=0;                             //if any error occured
                                    $arrayResponse["user_info"]=$arrayRetrievedUserInfo;    //array of the information retrieved from the database
                                    echo "**Checking contents of the arrayResponse using var_dump  <br>";
                                    var_dump($arrayResponse);
                                    echo "<br>---------------------------------------<br>";
                                    return $arrayResponse;                                  //return the array of response to the calling methode
                            }
                            else
                            {
                                    //Query failed to execute
                                    $arrayResponse["success"]=0;                            //status = failure
                                    $arrayResponse["errors"]=1;                             //errors occured =yes
                                    $arrayResponse["error_details"]=$arrayErrors;           //send the errors
                                    return $arrayResponse;                                  //return the array of response to the script: sign_in_android.php
                            }   


        }
        catch(Exception $e)
        {
        }
    }

输出是 ------------ 分别回显数据库元素hddhdh e dd ddd

88

使用 for 循环打印 arrayRetrievedUserinfo 的内容

88 天

**使用 var_dump 检查 arrayResponse 的内容

array(3) { ["success"]=> int(1) ["errors"]=> int(0) ["user_info"]=> array(4) { ["first_name"]=> string(6) "hddhdh" ["last_name"]=> string(1) "e" ["email"]=> string(3) "ddd" ["user_type"]=> string(1) "0" } }

4

1 回答 1

0

您存储数组的方法似乎是多余的,您可以尝试这种方法(避免使用mysql而不是使用mysqli):

$query = SELECT itemno FROM item; 
$result = mysqli_query($query); 
if($result) 
{ 
 $newrow[] = mysqli_fetch_row($result); 
}

$newrow[]是一个填充了数据库结果的数组。

所以你可以像这样循环你的记录:

for ($i=0; $i < count($newrow); $i++) 
print_r($newrow[$i]);

所以每个元素$newrow都是1个记录数组

于 2013-10-29T07:51:45.720 回答