2012-07-24 10:13



There are some topics narrowing my problem, but no one permits me to solve it.I have a csv file of terms from which i would like to count the number in a table of my db and to write the result in a new csv file. I need to create a new array, where each key would be a term of the first array and the value would be the related number of records in my database. Here are some examples of the input :



And i would like to get something like this:

lactose, 25

serine, 3

So i have created an array whose values are the terms in the csv file. Then i input the values of this array in a sql query. And then, i have been unable to find the right way on the Internet or in the php documentation. The last line completely fails, but i tried it because it seemed to be the narrower mean to manage my task.

Could someone help me ?

Thanks in advance !

$termslist= array();

$file = fopen('fileterms.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
  foreach ($line AS $molindice => $molvalues) 
    if ( ! in_array( $molvalue, $termslist ) )
            {array_push($termslist, $molvalues);}


//print_r ($termslist);

$molnumbers = array();

foreach ($termslist as $ancientkey => $ancientvalue)
    echo $ancientvalue;
    $sql3 = "SELECT content from mols WHERE content like '% $ancientvalue %'";
    $result3 = mysql_query($sql3)or die(mysql_error());
    $count3 = mysql_num_rows($result3);
    echo $count3;
    $molnumbers [$ancientvalue['molnames']] = $count3['quantities'];
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douqiao5314 douqiao5314 9年前

    In the line:

    if ( ! in_array( $molvalue, $termslist ) )

    You have a small error as it should be:

    if ( ! in_array( $molvalues, $termslist ) )

    Then in this line:

    $molnumbers [$ancientvalue['molnames']] = $count3['quantities'];

    $ancientvalue is already the value (or molname) itself and not an array by itself. Thus the line only needs to be:

        $molnumbers [$ancientvalue] = $count3['quantities'];

    then it should put the number into the correct part of the array. For the sql itself:

    $sql3 = "SELECT content from mols WHERE content like '% $ancientvalue %'";

    Here it seems to me that you are searching for %BLANKancientvalueBLANK% if that is not what you want to do there but instead to look for %ancientvalue%, you should modify the line to:

    $sql3 = 'SELECT content from mols WHERE content like \'%'.$ancientvalue.'%\'';
    点赞 评论 复制链接分享
  • dongzhidian3538 dongzhidian3538 9年前


    $molnumbers [$ancientvalue['molnames']] = $count3['quantities'];


    $molnumbers[$ancientvalue['molnames']] = $count3['quantities'];


    $molname = $ancientvalue['molnames'];
    $molnumbers[$molname] = $count3['quantities'];
    点赞 评论 复制链接分享