while循环返回在next for循环中重复的值

时间:2014-01-04 17:28:51

标签: php mysql arrays foreach while-loop

我的代码中发生的事情是我通过查询传递关键字。对于我想运行的每个关键字,该数组获取该关键字的结果并将它们插入到我的数据库中。

我遇到的问题是,它不是获取关键字的灵感,而是一旦它将该查询的值带入下一个,就会传递它。

所以我举个例子。值returning id = 8, 9, 10, 11, 12。然后插入的下一个查询应该只返回5个,但是对于下一个关键字,它会携带这些值。所以它可能是8, 9, 10, 11, 12, 13, 14, 15, 16, 17

如果我将该值提高到3。它将携带此8, 9, 10, 11, 12, 13, 14, 15, 16, 17并再添加五个条目。我不确定我的代码中发生了什么。

也许是一个更简单的例子。我查询了2个关键字的列表。然后,在这些关键字中,我需要通过sql2查询1个关键字,然后在所有这些值中将它们放入sql3。发生的事情是,它不是丢弃第一个关键字,而是第二次携带结果。因此,如果我查询关键字的灵感和启发。它将一次查询灵感,围绕它的第二个领域将查询灵感并激发灵感。而不只是查询启发第二次。

$sql = "
SELECT `id` , `title`
FROM `mf8uz_categories`
WHERE `extension` = 'com_hwdmediashare'
LIMIT 0 , 2";
$getInfo = mysql_query($sql);
//$numResults = mysql_num_rows($getInfo);
  while($keywordArray = mysql_fetch_array($getInfo))
    $keywords[] = $keywordArray;
    foreach($keywords as $keyword){ 
    $keyWord = $keyword['title'];
    $categoryId = $keyword['id'];
//  echo $categoryId . $keyWord . "</br>" . "</br>";
    $sql2 = "SELECT `id`, `title`,`description`
FROM `mf8uz_hwdms_media`
WHERE `title` LIKE '%$keyWord%'
OR `description` LIKE '%$keyWord%'
LIMIT 0 , 5";
//echo $sql2 . "<br>";
echo $keyWord . "-------";
$getResults = mysql_query($sql2);
//echo $getResults;
echo mysql_errno($db_server) . ": " . mysql_error($db_server) . "\n";
 while($row = mysql_fetch_array($getResults))
    $rows[] = $row;
    //echo $row . "</br>" . "HERE" ;
    foreach($rows as $row)  {
    $echoDescription = $row['description'];
    $echoRow = $row['title'];
    $videoId = $row['id'];
    echo $videoId . "</br>" . $echoDescription . "</br>" . $echoRow . "</br>";
//  echo "category ID equals" . $categoryId . " " . "video id is:" . $videoId ." ". "title is:" . $echoRow . " " . "Keyword Is:" . $keyWord . "</br>";

$sql3 = "INSERT INTO `btheman_dwjo21c4`.`mf8uz_hwdms_category_map` (
`id` ,
`element_type` ,
`element_id` ,
`category_id` ,
`ordering` ,
`created_user_id` ,
`created`
)
VALUES (
'', '1', '$videoId', '$categoryId', '0', '666', '2013-11-27 16:39:09'
);";

//$getResults = mysql_query($sql3);
//echo $sql3 . "  " . $echoRow . "</br>";
echo mysql_errno($db_server) . ": " . mysql_error($db_server) . "\n";
}
} 

1 个答案:

答案 0 :(得分:0)

我认为问题是你不是在每个循环中启动或重置$row。例如,这条线对我来说很突出:

 while($row = mysql_fetch_array($getResults))
    $rows[] = $row;
    //echo $row . "</br>" . "HERE" ;
    foreach($rows as $row)  {

您始终将项目添加到$rows。那么为什么不这样做呢:

 $rows = array();
 while($row = mysql_fetch_array($getResults))
    $rows[] = $row;
    //echo $row . "</br>" . "HERE" ;
    foreach($rows as $row)  {

我认为只需通过array()添加$rows = array();初始化即可解决此问题。但仍然在查看代码以查看是否还有其他内容。

编辑好的,在这里找到了另一个类似的问题:

//$numResults = mysql_num_rows($getInfo);
  while($keywordArray = mysql_fetch_array($getInfo))
    $keywords[] = $keywordArray;

注意$keywords[]但数组从未被引入或重置?这样做:

$keywords = array();
//$numResults = mysql_num_rows($getInfo);
  while($keywordArray = mysql_fetch_array($getInfo))
    $keywords[] = $keywordArray;

添加$keywords = array();将确保通过while($keywordArray = mysql_fetch_array($getInfo))的每个循环都会重置,以便为您提供正确的结果。

另一个编辑我重新格式化了您的代码 - 并在此过程中添加了我的修复程序 - 所以我可以更清楚地看到发生了什么。一般来说,每当你正确清理和格式化代码,使调试更容易和现场阻塞点。如果你碰到一个问题,你正在用头撞墙,看看你是否可以清理一下代码。在你知道之前,问题就解决了!

$sql = "SELECT `id` , `title`"
     . " FROM `mf8uz_categories`"
     . " WHERE `extension` = 'com_hwdmediashare'"
     . " LIMIT 0,2"
     ;

$getInfo = mysql_query($sql);
// $numResults = mysql_num_rows($getInfo);

$keywords = array();
while($keywordArray = mysql_fetch_array($getInfo)) {
  $keywords[] = $keywordArray;
}

foreach ($keywords as $keyword) { 
  $keyWord = $keyword['title'];
  $categoryId = $keyword['id'];
  // echo $categoryId . $keyWord . "</br>" . "</br>";
  $sql2 = "SELECT `id`, `title`,`description`"
        . " FROM `mf8uz_hwdms_media`"
        . " WHERE `title` LIKE '%$keyWord%'"
        . " OR `description` LIKE '%$keyWord%'"
        . " LIMIT 0 , 5"
        ;
  // echo $sql2 . "<br>";
  echo $keyWord . "-------";
  $getResults = mysql_query($sql2);
  //echo $getResults;
  echo mysql_errno($db_server) . ": " . mysql_error($db_server) . "\n";

  $rows = array();
  while($row = mysql_fetch_array($getResults)) {
    $rows[] = $row;
  }

  //echo $row . "</br>" . "HERE" ;
  foreach($rows as $row)  {
    $echoDescription = $row['description'];
    $echoRow = $row['title'];
    $videoId = $row['id'];
    echo $videoId . "</br>" . $echoDescription . "</br>" . $echoRow . "</br>";
    //  echo "category ID equals" . $categoryId . " " . "video id is:" . $videoId ." ". "title is:" . $echoRow . " " . "Keyword Is:" . $keyWord . "</br>";

    $sql3 = "INSERT INTO `btheman_dwjo21c4`.`mf8uz_hwdms_category_map` (
    `id` ,
    `element_type` ,
    `element_id` ,
    `category_id` ,
    `ordering` ,
    `created_user_id` ,
    `created`
    )
    VALUES (
    '', '1', '$videoId', '$categoryId', '0', '666', '2013-11-27 16:39:09'
    );";

    //$getResults = mysql_query($sql3);
    //echo $sql3 . "  " . $echoRow . "</br>";
    echo mysql_errno($db_server) . ": " . mysql_error($db_server) . "\n";
  } // end foreach '$rows'

} // end foreach '$keywords'