March 3, 2012

php mysql select with array in to array

Question by maanu

I need to select ids from database with arrays.
my english not good. I think the best way to show my codes

the form result print_r($malayalam); like this Array ( [0] => helo [1] => hi[2] => how)

I need to select its ids from table. my code is not correct. any way let me show you here

$results=mysql_query("SELECT ml_id FROM ml_table WHERE word = '$malayalam'"); 
$numrows=mysql_num_rows($results);
if($numrows!=0){
    $ml_row = mysql_fetch_array($results);
    $ml_id = $ml_row['ml_id'] ;
    echo "Malayalam ID " . $ml_id . "<br />";
}

I need to add all my result in to another array.

is that possible ?
if u have any idea could you answer to me please

Answer by maanu

finally fainally found solution with the help of answers

$rArray = mysql_query("SELECT ml_id FROM ml_table WHERE word IN ('".implode("', '", $malayalam)."')"); 
if(mysql_num_rows($rArray)>0){
    $temp_rows = array();       
    while(($row = mysql_fetch_array($rArray))) {
        $temp_rows[] = $row['ml_id'];
    }
}

the result of print_r($temp_rows) coming like this Array ( [0] => 123 [1] => 234 [2] => 312)

thank to all

Answer by Starx

If I understood properly, the following is what you need

$results=mysql_query("SELECT * FROM ml_table WHERE word = '$malayalam'"); 
if(mysql_num_rows($results)>0){

    $newArray = array(); //Create a new array

    while($ml_row = mysql_fetch_array($results)) {
        $ml_id = $ml_row['ml_id'] ;
        $newArray[$ml_id] = $ml_row;
        echo "Malayalam ID " . $ml_id . "<br />";
    }

    //$newArray is your new array accesible from id

}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!