1

I made a loop which take from a value e.g. #test #top #travel (from a variable) and split in each array to show me all the results which match with the keyword, but the query show me only the results for the last key e.g #travel and I want to show for each key the query which match with the statement. Sorry if is a easy question, but I really search and nothing...

//the loop

$keywordArray = explode(' ',  $keyword);
foreach ($keywordArray as $keywordArrayGo) {
    echo $keywordArrayGo;
    $sqlKeywordArray = "SELECT id, titlu, link, poza, alt, keywords, linknews FROM stiinta WHERE   approved='1' AND  replace(replace(replace(keywords, ',', ''), '-', ' '), ' ', '') LIKE  concat('%', ? , '%')  UNION SELECT id, titlu, link, poza, alt, keywords, linknews FROM travel WHERE   approved='1' AND  replace(replace(replace(keywords, ',', ''), '-', ''), ' ', '') LIKE  concat('%', ? , '%')  UNION SELECT id, titlu, link, poza, alt, keywords, linknews FROM natura WHERE   approved='1' AND  replace(replace(replace(keywords, ',', ''), '-', ''), ' ', '') LIKE  concat('%', ? , '%')  UNION SELECT id, titlu, link, poza, alt, keywords, linknews FROM travel WHERE   approved='1' AND  replace(replace(replace(keywords, ',', ''), '-', ''), ' ', '') LIKE  concat('%', ? , '%')  UNION SELECT id, titlu, link, poza, alt, keywords, linknews FROM lifestyle WHERE   approved='1' AND  replace(replace(replace(keywords, ',', ''), '-', ''), ' ', '') LIKE  concat('%', ? , '%')  LIMIT 10";  

the prepare stmt

    $stmt = $con->prepare($sqlKeywordArray);
    $stmt->bind_param("sssss", $keywordArrayGo, $keywordArrayGo, $keywordArrayGo, $keywordArrayGo, $keywordArrayGo); 
    $stmt->execute();

check the results for each key.

    $stmt->store_result();
    /* Get the number of rows */
    $checkQuery = $stmt->num_rows;
    echo  "$keywordArrayGo : $checkQuery : QUERY care sa se potriveasca <br>";
    if ($checkQuery == 0) {// verificare daca este vrun query
      echo "<FONT color='red'><strong>NU SE POTRIVESTE NICIO INREGISTRARE.</strong><br></font>";
    }

the $checkQuery for the value e.g. (#top #test #travel) is (10 2 4)

and show results which match with the key split using explode()

    $stmt->bind_result($idSearch, $titluKEY, $linkKEY, $pozaKEY, $altKEY, $keywordKEY, $linknewsKEY);//variabla pe care o vrei inlocuita prin bind_Result in loc de get_Result
    $keywordArticleMatch ="";
    while ($stmt->fetch())    {
       // faci acelasi lucru fara $row trb sa fie in concordanta cu ceea ce este in SELECT column pentretu teste a href="/page-stiinta.php?pid='.$linkKEY.'"
      $keywordArticleMatch .= '<div id="articol-content-more"><a href="/'.$linknewsKEY.'"><img src="/images/'.$pozaKEY.'.jpg"class="articol-content-more-image" alt="'.$altKEY.'"><p class="articol-content-more-title">'.$titluKEY.'</p></a><span><a class="articol-content-more-afla" href="/'.$linknewsKEY.'">Citește mai multe</a></span><span class="articol-content-more-fl"><div class="fb-share-button" data-layout="button_count" data-href="http://esticurios.ro/'.$linknewsKEY.'"></div></span></div><br>';      
    }
    /* free results */
    $stmt->free_result();
    $stmt->close();
} //close the loop

And the problem is showing me only the results of the last key from the value AND i want to make this to show for the all key, not only from the last key splited

Stefan
  • 197
  • 1
  • 8
  • Today, natively, there's no way to do that. But in a near future, maybe: https://wiki.php.net/rfc/debugging_pdo_prepared_statement_emulation (it's for PDO, not for MySQLi) Look also for: http://stackoverflow.com/questions/962986/how-to-echo-a-mysqli-prepared-statement – Gabriel Heming Dec 07 '16 at 18:22
  • I dont think is not possible is using a second loop, but i dont know how and where to put that – Stefan Dec 07 '16 at 18:38
  • I think I did a huge mystake about your problem and, as well, I think your problem is here `$keywordArticleMatch ="";` In each loop you are reseting the variable. – Gabriel Heming Dec 07 '16 at 19:20
  • and how, I can fix that to not resert the variable? I want to show all the query from all the key – Stefan Dec 07 '16 at 20:40

0 Answers0