Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey,

for a project I have to fetch the following array from my DB

Array
(
[admin] => Array ( [password] => admin [level] => 99 )
[test] => Array ( [password] => test [level] => 1 )
...
)

What I'm recieving however looks like this:

Array
(
[..] => Array ( [password] => [level] => )
[0] => testmail1@mail.com [1] => Array ( [password] => 1234567 [level] => 99 )
[2] => testmail2@mail.com [3] => Array ( [password] => 7654321 [level] => 55 )
...
)

The problem is the appearance of the [0]=> , [1]=> etc in each row.

My code is the following one

XML
$users=array ( "$email" => array("password" =>$passwd,  "level"=>$level  ) );
echo "<hr>";

            $result = @mysql_query( "select email, passwd, level from personen order by id" ) or die("Invalid query 1: " . mysql_error());
                for ($i=0; $i<@mysql_num_rows($result); $i++)
            {
                $k=@mysql_fetch_array ($result);
                $email= "$k[email]";
                $passwd="$k[passwd]";
                $level="$k[level]";
                $loginfo = array ("password"=>$passwd, "level"=>$level );
                array_push($users, $email, $loginfo );

            }
     print_r ($users); echo "<hr>";


Anyone got an idea whats wrong?

Thanks in advance
Posted

Your whole code is confusing.
use mysql_fetch_assoc instead of mysql_fetch_array. result is same mysql_fetch_array has extra index to browse with integer value instead of using array_push you can use simple $users[index]. but yet your whole idea is misleading. can't help you cause don't know what do you need
 
Share this answer
 
Comments
Stanislav Jakunschin 19-Jun-12 7:29am    
<pre lang="PHP">
$result = @mysql_query( "select email, passwd, level from personen order by id" ) or die("Invalid query 1: " . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo $row["email"]."<br>";
echo $row["passwd"]."<br>";
echo $row["level"]."<br>";
$users = array($row["email"]=> array ("password"=>$row["passwd"], "level"=> $row["level"]));
}
mysql_free_result($result);
</pre>

Output:
test1@gmx.eu
827ccb0eea8a706c4c34a16891f84e7b
99
...
...
5
admin@gv-service.eu
827ccb0eea8a706c4c34a16891f84e7b
1

Array ( [admin@gv-service.eu] => Array ( [password] => 827ccb0eea8a706c4c34a16891f84e7b [level] => 1 ) )



The correct result in Array $users only for the last sample.

How to do that to all of the previous sample fell into the arrays $users?,

<pre lang="PHP">
$result = @mysql_query( "select email, passwd, level from personen order by id" ) or die("Invalid query 1: " . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo $row["email"]."<br>";
echo $row["passwd"]."<br>";
echo $row["level"]."<br>";
$users = array($row["email"]=> array ("password"=>$row["passwd"], "level"=> $row["level"]));
}
mysql_free_result($result);
</pre>

Output:
test1@gmx.eu
827ccb0eea8a706c4c34a16891f84e7b
99
...
...
5
admin@gv-service.eu
827ccb0eea8a706c4c34a16891f84e7b
1

Array ( [admin@gv-service.eu] => Array ( [password] => 827ccb0eea8a706c4c34a16891f84e7b [level] => 1 ) )



The correct result in Array $users only for the last sample.

How to do that to all of the previous sample fell into the arrays $users?
I don't have PHP installed on my work PC so I cannot test the following. However, I believe this is a normal behavior for the array_push function. Below is one of the comments from the PHP Manual.

From the manual:

If you're going to use array_push() to insert a "$key" => "$value" pair into an array, it can be done using the following:
$data[$key] = $value;
It is not necessary to use array_push.


Here is the link.

http://php.net/manual/en/function.array-push.php[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900