0

I have a question on how to go about the next phase of a project I am working on.

Phase I:

create a php script that scraped directory for all .txt file.. Open/parse each line, explode into array... Loop through array picking out pieces of data that were needed and INSERTING everything into the database (120+ .txt files & 100k records inserted)..

this leads me to my next step,

Phase II:

I need to take a 'list' of several 10's of thousand of numbers.. loop through each one, using that piece of data (number) as the search term to QUERY the database.. if a match is found I need to grab a piece of data in a different column of the same record/row..

General thoughts/steps I plan to take

  1. scrape directory to find 'source' text file.
  2. open/parse 'source file'.... line by line...
  3. explode each line by its delimiting character.. and grab the 'target search number'
  4. dump each number into a 'master list' array...
  5. loop through my 'master list' array.. using each number in my search (SELECT) statement..
  6. if a match is found, grab a piece of data in another column in the matching/returned row (record)...
  7. output this data.. either to screen or .txt file (havent decided on that step yet,..most likely text file through each returned number on a new line)

Specifics:

I am not sure how to go about doing a 'multiple' search/select statement like this?

How can I do multiple SELECT statements each with a unique search term? and also collect the returned column data?

is the DB fast enough to return the matching value/data in a loop like this? Do I need to wait/pause/delay somehow for the return data before iterating through the loop again?

thanks!

current function I am using/trying:

this is where I am currently:

$harNumArray2 = implode(',', $harNumArray);
//$harNumArray2 = '"' . implode('","', $harNumArray) . '"';
$query = "SELECT guar_nu FROM placements WHERE har_id IN ($harNumArray2)";
echo $query;

$match = mysql_query($query);
//$match = mysql_query('"' . $query . '"');
$results = $match;
echo("<BR><BR>");
print_r($results);

I get these outputs respectively:

Array ( [0] => sample_source.txt )

Total FILES TO GRAB HAR ID's FROM: 1

TOAL HARS FOUND IN ALL FILES: 5 SELECT guar_nu FROM placements WHERE har_id IN ("108383442","106620416","109570835","109700427","100022236")

&

Array ( [0] => sample_source.txt )

Total FILES TO GRAB HAR ID's FROM: 1

TOAL HARS FOUND IN ALL FILES: 5 SELECT guar_nu FROM placements WHERE har_id IN (108383442,106620416,109570835,109700427,100022236)

Where do I stick this to actually execute it now?

thanks!

update:

this code seems to be working 'ok'.. but I dont understand on how to handle the retirned data correctly.. I seem to only be outputting (printing) the last variable/rows data..instead of the entire list..

$harNumArray2 = implode(',', $harNumArray);
//$harNumArray2 = '"' . implode('","', $harNumArray) . '"';
//$query = "'SELECT guar_num FROM placements WHERE har_id IN ($harNumArray2)'";

$result = mysql_query("SELECT har_id, guar_num FROM placements WHERE har_id IN (" . $harNumArray2 . ")")
//$result = mysql_query("SELECT har_id, guar_num FROM placements WHERE har_id IN (0108383442,0106620416)")
or die(mysql_error());  

// store the record of the "example" table into $row
$row = mysql_fetch_array($result);
$numRows = mysql_num_rows($result);

/*
while($row = @mysql_fetch_assoc($result) ){
    // do something
    echo("something <BR>");
}
*/

// Print out the contents of the entry 
echo("TOTAL ROWS RETURNED : " . $numRows . "<BR>");
echo "HAR ID: ".$row['har_id'];
echo " GUAR ID: ".$row['guar_num'];

How do I handle this returned data properly?

thanks!

4

2 回答 2

0

我不知道这是否回答了您的问题,但我认为您是在询问子查询。它们非常简单,看起来像这样

SELECT * FROM tbl1 WHERE id = (SELECT num FROM tbl2 WHERE id = 1);

只有当第二个子查询有一个唯一值时,这才有效。如果它返回多行,它将返回一个解析错误。如果您必须选择多行,请研究 JOIN 语句。这可以让你开始

http://www.w3schools.com/sql/sql_join.asp

于 2013-03-19T15:31:06.640 回答
-1

我不确定如何进行这样的“多个”搜索/选择语句?

关于多选,(我假设您使用的是 MySQL),您可以使用“ IN”关键字简单地执行该操作:

例如:

SELECT * 
FROM YOUR_TABLE 
WHERE COLUMN_NAME IN (LIST, OF, SEARCH, VALUES, SEPARATED, BY COMMAS)

编辑:按照问题中的更新代码。

在我们继续之前……你应该尽量避免在 PHP 中为新代码使用 mysql_ 函数,因为它们即将被弃用。考虑使用通用 PHP DB 处理程序 PDO 或更新的 mysqli_ 函数。有关为您选择“正确”API 的更多帮助在这里

如何正确处理返回的数据?

要处理多行数据(您就是这样),您应该使用循环。应该执行以下操作(我的示例将使用 mysqli_ 函数 - 这可能与您一直在使用的 API 更相似):

  $mysqli   = mysqli_connect("localhost", "user", "pass");
  mysqli_select_db($mysqli, "YOUR_DB");

  // make a comma separated list of the $ids.
  $ids = join(", ", $id_list);

  // note: you need to pass the db connection to many of these methods with the mysqli_ API
  $results  = mysqli_query($mysqli, "SELECT har_id, guar_num FROM placements WHERE har_id IN ($ids)");
  $num_rows = mysqli_num_rows($results);

  while ($row = mysqli_fetch_assoc($results)) {
    echo "HAR_ID: ". $row["har_id"]. "\tGUAR_NUM: " . $row["guar_num"] . "\n";
  }  

请注意,这是非常基本的(并且未经测试!)代码,只是为了显示最低限度的步骤。:)

于 2013-03-19T15:33:20.837 回答