0

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

hakre
  • 193,403
  • 52
  • 435
  • 836
Mo.
  • 26,306
  • 36
  • 159
  • 225
  • What does the table data look like? What are the names of the columns? I don't think the $malayalam array is going to work in the SQL SELECT statement the way you want it to either. – Shaun Bohannon Mar 03 '12 at 17:04
  • @sbohan Hi table name is ml_table columns are ml_id, word the thing is that I need to select ml_id from thable which are equal to `Array ( [0] => helo [1] => hi[2] => how)` – Mo. Mar 03 '12 at 18:22

4 Answers4

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

Now when you printr($ml_ad); it will show all your id's in an array.

Manigandan Arjunan
  • 2,260
  • 1
  • 25
  • 42
0

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

}
Starx
  • 77,474
  • 47
  • 185
  • 261
  • Try to run the command, alone using phpmyadmin only and see if outputs anything. – Starx Mar 03 '12 at 18:46
  • I think the problem on $malayalam. bcos it is also array. – Mo. Mar 03 '12 at 18:48
  • @maanu, of course, thats a problem. It is not designed to read an array. – Starx Mar 03 '12 at 18:51
  • do u know how to select with array ? – Mo. Mar 03 '12 at 19:04
  • There might be a way to do so, but i need to know, what kind of array it is? and what sort of comparison you need? – Starx Mar 03 '12 at 19:06
  • actually it is for a form i can show you the link http://www.chatfitness.com/add_english.php. I need to add new words. this function for checking that this word already exist in the table – Mo. Mar 03 '12 at 19:28
  • @maanu, So then why do you need to pass it as an array. Pass it as a single word, instead of any array. Extract it using something like `$arr[0]` and pass it to the query. – Starx Mar 03 '12 at 19:32
  • I need to collect all `ml_id` to use another function. – Mo. Mar 03 '12 at 19:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8487/discussion-between-maanu-and-starx) – Mo. Mar 03 '12 at 19:40
  • I am so sorry. It was already 1 o clock at night, had to sleep. – Starx Mar 04 '12 at 00:09
0

You should write something like this:

$ids = array();
$in = '"' . implode('","', array_map('mysql_real_escape_string', $malayalam)) . '"';
$result = mysql_query('SELECT id FROM ml_table WHERE word IN(' . $in . ')');
while($row = mysql_fetch_assoc($result)) {
    $ids[] = $row['id'];
}

$anotherArray = array_merge($ids, $anotherArray);
OR
$anotherArray += $ids;
Vitaly Muminov
  • 1,914
  • 12
  • 11
0

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

Mo.
  • 26,306
  • 36
  • 159
  • 225