0

In the following code i am assigning the 6 values extracted from the database into and array 'arrayRetrievedUserInfo' ; but if I print this array it shows only two of them. Next I am assigning this array into another array 'arrayResponse' ; this array contains arrayRetrievedUserInfo and two other fields . But if i print this array it shows the four values of arrayRetrievedUserInfo which were not shown while printing arrayRetrievedUserInfo directly . Please help me to solve this issue.

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)
        {
        }
    }

The output is ------------ Echoing database elements individuallyhddhdh e dd ddd

88

Printing the contents of the arrayRetrievedUserinfo using a for loop

88 dd

**Checking contents of the arrayResponse using var_dump

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" } }

2
  • Why do you want to use two arrays for storing values? Following assignment $arrayRetrievedUserinfo=mysql_fetch_array($result_sign_in) instead of $row=mysql_fetch_array($result_sign_in) should resolve your issue. Commented Oct 29, 2013 at 7:44
  • @learningloop , yeah you are right , but still I am wondering why my code is not working , can you please explain!!! Commented Oct 29, 2013 at 9:29

1 Answer 1

0

Your approach of storing array seems to be redundant instead you can try this approach(Avoid using mysql instead use mysqli):

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

$newrow[] is an array filled with database results.

so you can loop over your records like so:

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

so every element in $newrow is 1 record array

Sign up to request clarification or add additional context in comments.

4 Comments

This is working , but can you explain what is wrong in my code... the array seem to contain all the data but it is not showing
Difference occurs because of mysql_fetch_row,which fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0...Check here for more info:stackoverflow.com/questions/8347565/…
That is ok , but in my code $row[] is showing its contents but the same values if i assign to a different array as newArray['first']=$row['first'] and so on , then it does not show all... is this an abnormal behaviour ? or I have done something wrong?
It behaves differently if use mysql_fetch_array.Even I am not aware of exact issue here but i always use mysql_fetch_row if i have to store query results into array.If it works then you can accept the answer as well..:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.