Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数据库中搜索数组中的值_Php_Database_Arrays_Search - Fatal编程技术网

Php 从数据库中搜索数组中的值

Php 从数据库中搜索数组中的值,php,database,arrays,search,Php,Database,Arrays,Search,我有一个数据库,里面有员工ID和每个员工ID对应的员工姓名。有没有办法从数据库中搜索员工ID的数组?谷歌帮不了我,我想是因为我不知道如何用词来形容我的搜索 我的想法是使用类似于array\u search($empID,$currentary)的东西。然后循环遍历数据库中的每个员工ID,并将其与$currentArray进行比较?我怀疑这是最有效的方法,但我仍在学习,因此任何帮助都将不胜感激。如果有人愿意花点时间来帮助我,我可以发布更多需要的信息。谢谢 如果有人感兴趣,请编辑我的代码: <

我有一个数据库,里面有员工ID和每个员工ID对应的员工姓名。有没有办法从数据库中搜索员工ID的数组?谷歌帮不了我,我想是因为我不知道如何用词来形容我的搜索

我的想法是使用类似于array\u search($empID,$currentary)的东西。然后循环遍历数据库中的每个员工ID,并将其与$currentArray进行比较?我怀疑这是最有效的方法,但我仍在学习,因此任何帮助都将不胜感激。如果有人愿意花点时间来帮助我,我可以发布更多需要的信息。谢谢

如果有人感兴趣,请编辑我的代码:

 <?php 

//this variable tells us how many drupal nodes or 'paystub pages' we need to create
$nodeCount = 0;
$i = 0;

//needed for creating a drupal node
//for this code to work this script must be run from the root of the drupal installation
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

if ($handle = opendir('/var/www/html/pay.mistequaygroup.com/upload')) 
{

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) 
    {
       if ($file != "." && $file != "..") 
       {
            $nodeCount++;
            //We convert the pdf documents into text documents and move put them in the converted folder
            $command = "pdftotext /var/www/html/pay.mistequaygroup.com/upload/" . $file . " /var/www/html/pay.mistequaygroup.com/upload/converted/" . $file . ".txt";
            //Execute the command above
            $output = exec($command);

            //mark all the spots that TO THE ORDER OF shows up
            //echo array_search("TO THE ORDER OF", $currentArray);

            //echo $userName;


            //extract the employees name


            //print_r($currentArray);
            //echo '<pre>';
            //echo array_search("DATE AMOUNT", $currentArray);
            //echo '</pre>';

        }    
    }        
    closedir($handle);        
}

//subtract two because the folders "array" and "converted" are included because PHP does not differentiate
//between folders and files
$nodeCount = $nodeCount - 2; 

echo "<br />";
echo "I counted $nodeCount pdf files";
echo "<br />";

//open the directory
if ($handle2 = opendir('/var/www/html/pay.mistequaygroup.com/upload/converted')) 
{
    //check to see if we have reached the last file of our directory, if not stay in loop
    while (false !== ($currentText = readdir($handle2))) 
    {
        //filter out files named . and ..
       if ($currentText != "." && $currentText != "..") 
       {
               //Create a file for array to be printed to
               $createArray = fopen("/var/www/html/pay.mistequaygroup.com/upload/arrays/" . $currentText . ".txt", "w+") or die ("Cannot find file to create array, ID 2");


               //read the file we are on from the loop into the array 
               $currentArray = file("/var/www/html/pay.mistequaygroup.com/upload/converted/" . $currentText, FILE_SKIP_EMPTY_LINES) or die ("Cannot find file to create array, ID 1");

               //$countArray = array_search(". . . . . . . . . .", $currentArray);

               //echo $countArray;

               //print array to .txt file for debugging purposes
               $out = print_r($currentArray, true);
               fwrite($createArray, $out);
               fclose($createArray);


               //Loop?
            array_search($empID, $currentArray);

            //need to loop through the array we are on, looking for numbers that match the employee ID's 
            //OR we might have to search for names within a string of text and then get the corresponding ID for that user from the database? 

            //brainstorming 
               $query = SELECT * FROM `profile_values` WHERE `fid` = 2 AND `value` = $employeeID; 



               //DOES NOT WORK AS EXPECTED
               $indexEmpid = 0;
               foreach ($currentArray as $value)
               {
                   //set the value to 28 and it doubles each time the loop runs so there is no need to add 28 each time
                   //every 28th index in our array is an employee id
                   $indexEmpid = $indexEmpid + 28;
                   $currentEmployeeID = $currentArray[$indexEmpid];
                   echo "<br />";
                   echo "Employee ID's found: $currentEmployeeID";
                //echo "Employee ID's found: $currentArray[$indexEmpid]";
                echo "<br />";
                echo "IndexEmpid: $indexEmpid";            
               }




       }
     }

}


?>

使用:
其中employeeID位于(id列表…)
(用逗号分隔)


更多信息请参见:

是的,您可以使用SQL
in
子句进行操作,如下所示:


对不起,我说得不够清楚。这里真正发生的事情是,我正试图导入工资存根。我通过将PDF文件转换为txt,然后将txt文件读入PHP中的数组来实现这一点。例如,我不能每隔28行就获取员工ID,因为ID不是数组中的每28行索引。所以我认为下一个最好的方法是搜索ID。我有一个drupal数据库,每个用户名都有一个员工ID。我想从我的数据库中搜索我的工资存根数组,寻找匹配的员工ID。如果这有道理的话。
$empIDs = array(1,2,3);
$query = "SELECT name FROM emp WHERE ID IN (" . implode(',',$empIDs) . ")";