0

i want to get an array like this:

$autocompletiondata = array(
    3 => 'Hazel Grouse',
    4 => 'Common Quail',
    5 => 'Common Pheasant',
    6 => 'Northern Shoveler',
    7 => 'Greylag Goose',
    8 => 'Barnacle Goose');

out off this SQL-Query:

SELECT id, name FROM tbl_1

Can anyone tell me how to do this?

2 Answers 2

3
$autocompletiondata = array();

$sql = "SELECT id, name FROM tbl_1";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {
   $autocompletiondata[$row['id']] = $row['name'];
}

This presumes that you've established a connection to the database, etc...

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

1 Comment

Thanks for you fast answer :) was exactly what ive been looking for
0
$autocompletiondata =  array(); 

$query  = "SELECT id, name FROM tbl_1";           
$result = mysql_query($query) or die(mysql_error());

while(list($id, $name) = mysql_fetch_array($result))
{
$autocompletiondata[$id] = $name; 
}

easier to read :)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.