3

Am trying to run this code:

foreach ($tags as $tag)
{   
    $tag = strtolower($tag);
    $tag = trim($tag);
    echo "<br>" . $tag . " checking<br>";

    $query = "SELECT `tag_id`, `tag_name` FROM TAG where `tag_name` = ?";
    $stmt = mysqli_prepare($connection, $query);
    $stmt->bind_param("s", $tag);

    if ($stmt->execute())
    {
        echo "Success";
    }
    else
    {
        echo "Error";
    }
}

My $tags array has food, school, Birthday, not, There, over, Limit.

The first three elements food, school, Birthday are in my TAG table

The output I get is

food checking
Success
school checking

Then the page crashes with a 500 error and doesn't continue loading. Am really confused on what could be breaking the code here.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mina Messiah
  • 119
  • 6
  • Running a query inside a for loop will make code very slow. Try using MySQL IN() function. https://www.w3resource.com/mysql/comparision-functions-and-operators/in-function.php – vimuth May 16 '20 at 14:04
  • @ArunaPerera Do you think that could be what's breaking the code? – Mina Messiah May 16 '20 at 14:06
  • Running a query in a loop might not be best practice, but it definitely shouldn't break (especially on the second element). If you're getting a 500 response, there should be more information in the error log about what's actually going wrong. Have a look here: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – iainn May 16 '20 at 14:09
  • I ask to do 2 things, 1. try to with only 3 elements on your `$tags` array because `not` is a keyword of mysql and I suspect that my break your code, 2. Use `IN()` clause instead of `foreach()` loop – A l w a y s S u n n y May 16 '20 at 14:10
  • @AlwaysSunny What? `not` is part of the data, not SQL. How can it break SQL? – Dharman May 16 '20 at 15:08
  • The page crashes for a simple reason that you never finished the first statement. You need to add `get_result` after execute. I recommend using PDO instead, because it is simpler to use and you would not make such a simple mistake. – Dharman May 16 '20 at 15:10
  • @yourCommonSense Could you add a second duplicate explaining why you should fetch results from the statement when SELECTing? Maybe this one? https://stackoverflow.com/q/614671/1839439 I can't find a good one anywhere. or this one is also good: https://stackoverflow.com/questions/35536580/php-commands-out-of-sync-you-cant-run-this-command-now/35537106#35537106 – Dharman May 16 '20 at 15:15
  • @Dharman my policy is to keep as one dupe target as possible. Without an answer and with a single duplicate a post will become a redirect for the unprivileged user, thus reducing the amount of questions indexed and displayed, making it alomst as good as deleted. For me, such a possibility outweigh the garland of dupes at the top. After all, a helpful link posted in the comments is as good as posted as a dupe. What do you think? – Your Common Sense May 16 '20 at 15:22

0 Answers0