0

I have a mysql query which is return matching data from db. I need to store matching results data to $autocompletiondata like that :

$autocompletiondata =  array(
        1 => "shibbir",
        2 => "ahmed",
        3 => "babu",
        4 => "rahim",
        5 => "shakil",
);

Sql query :

$sql = mysqli_query($link, "SELECT cdid, family_name FROM contact_details WHERE     
family_name LIKE '%$term' ");

while($res = mysqli_fetch_array($sql)){
    $cdid = $res['cdid'];
    $fname = $res['family_name'];    
    $autocompletiondata =  array(
        $cdid => "$fname");
}

How can I store all matching data to associative array ? Plz help.

1

5 Answers 5

3

Can you try this?

$autocompletiondata = array();

while($res = mysqli_fetch_array($sql)){
    $cdid = $res['cdid'];
    $fname = $res['family_name'];    
    $autocompletiondata[$cdid] =  $fname;
}
Sign up to request clarification or add additional context in comments.

Comments

0
while($res = mysqli_fetch_array($sql)){
  $cdid = $res['cdid'];
  $fname = $res['family_name'];    
  $autocompletiondata[$cdid] = $fname; // <== make this change to your code
}

Comments

0

Try this

$sql = mysqli_query($link, "SELECT cdid, family_name FROM contact_details WHERE     
family_name LIKE '%$term' ");

while($res = mysqli_fetch_array($sql)){
    $cdid = $res['cdid'];
    $fname = $res['family_name'];    
    $autocompletiondata[$cdid] = $fname;
}

And print the array with

print_r($autocompletiondata);

Comments

0

Its simple just use the code below

$autocompletiondata =  array();
$sql = mysqli_query($link, "SELECT cdid, family_name FROM contact_details WHERE     
family_name LIKE '%$term' ");

while($res = mysqli_fetch_array($sql)){
    $cdid = $res['cdid'];
    $fname = $res['family_name'];    
    $autocompletiondata[$cdid] = $fname;
}

Hope this helps you

Comments

0

Just set data to $autocompletiondata:

$autocompletiondata = array();
while($res = mysqli_fetch_array($sql)){
    $cdid = $res['cdid'];
    $fname = $res['family_name'];    
    $autocompletiondata [$cdid] = $fname;
}

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.