将数据库中的值插入到数组中

时间:2013-10-29 07:35:00

标签: php

在下面的代码中,我将从数据库中提取的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)
        {
        }
    }

输出是------------ 独立回应数据库元素 Ë DD DDD

88

使用for循环

打印arrayRetrievedUserinfo的内容

88 dd

**使用var_dump

检查arrayResponse的内容

array(3){[“success”] =&gt; int(1)[“errors”] =&gt; int(0)[“user_info”] =&gt; array(4){[“first_name”] =&gt; string(6)“hddhdh”[“last_name”] =&gt; string(1)“e”[“email”] =&gt; string(3)“ddd”[“user_type”] =&gt; string(1)“0”}}

1 个答案:

答案 0 :(得分: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个记录数组