tags:

views:

88

answers:

5

i want to my array ,

somthing like this manner

array("userid"=>"username","1"=>"ganeshfriends","2"=>"tester")

mysq query somthing like this

$query = select username, userid from tbluser

$result = mysql_query($query);

while($row = mysql_fetch_array($result)){
    $items = array($row['userid']=>$row['username']);
}
print_r($items);

Can you tell me how to make

userid as key and username as val...

Thanks

+4  A: 

Try this:

$query = "select username, userid from tbluser";

$result = mysql_query($query);
$items=array();
while($row = mysql_fetch_array($result)){
    $items[$row['userid']]=$row['username'];
}
print_r($items);

The problem is that you delete and reassign the result array ($items) in every loop of the while block

mck89
I don't understand the OP's question - `$items[key] = value` is identical to `$items = Array(key => value)` with the exception that the former will append to `$items` if it is already defined, while the latter will make `$items` a new array. Also you've got a stray bracket after that line ;-)
Andy Shellam
Shouldn't it be `$items[$row['userid']]=$row['username']);` or is this a notation style I'm unfamiliar with?
Anthony
+1 for fixing it.
Anthony
Sorry i forgot to delete the ")"
mck89
@Andy Shellam: with the Array(key => value) form you reassign the $item variable every time while with $items[key] = value you create a new element in the array because the key is different every time
mck89
@mck89 - check out my pretty version of your answer.
Anthony
@mck89 - I did point that out in my comment!
Andy Shellam
@Andy Shellam: Sorry i missed that part
mck89
+1  A: 

You can do:

$arr = array('userid' => 'username');

$result = mysql_query($query);
if(!$result) die("Query failed");

while($row = mysql_fetch_array($result)){
  $arr[$row['userid']] = $row['username'];
}
codaddict
+3  A: 

Since everyone keeps suggesting the same thing, I figure I may as well join in.

When I have a situation with keys from sql values, I like to set them to variables first so it's nicer looking:

$query = "select username, userid from tbluser";

$result = mysql_query($query);

while($row = mysql_fetch_array($result)){
    $userid = $row['userid'];
    $username = $row['username']
    $items[$userid] = $username;
}
print_r($items);

It may add two lines, but it sure is easier on the eyes and avoids brackets in brackets.

Anthony
A: 

Use other variables, like Anthony, i just want to say, don't remember about stripslashes(i think you've done mysql_real_escape_string() when writing them to tha database)


while($row = mysql_fetch_array($result)){
    $userid = $row['userid'];
    $username = stripslashes($row['username']);
    $items[$userid] = $username;
}
Syom
mysql_real_escape_string() escapes values in the context of a query, it does not leave slashes in the database. A query of database values will return the value *without* slashes. Doing stripslashes() at that point will just remove legitimate slashes, instead.
pinkgothic
when he writes "username" into database, (when using mysql_query("insert into ...")), he must use mysql_real_escape_string, for security reasons. to make safe query.so then he must use stripslashes...
Syom
A: 

since youre only using associative access i would propose the use of mysql_fetch_assoc() instead of mysql_fetch_array()

since you don't arg that you want an associative array given back by mysql_fetch_array() it creates 2 arrays, one with int-indexes and one with string-indexes (assoc[iative]), even though this short script doesn't use much memory and might have high speed, you should always remember the performance.

Jaysn