I have the following code:
$indtag = '';
foreach($pretag as &$indtag) { //cycles through tags, puts quotes into check by tag
$quote = mysqli_query($mysqli, "SELECT `id`, `$indtag` FROM `beyonce` WHERE `$indtag` LIKE '%$indtag%'");
while($row = mysqli_fetch_assoc($quote)) {
echo $row['$indtag'];
echo $row['id'];
}
}
The table has fields for ids, quotes, then an individual column for each tag (ang for anger being an example). pretag is an array full of tags (rom is romance, ang is anger, dece is deception) that I'm running through, trying to find quotes with those IDs and tags. The statement works fine in SQL when I run it with ang, it selects the IDs fine, but when I try to select the column/field for a tag using a variable, nothing comes back. Any ideas?
mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use string interpolation to accomplish this because you will create severe SQL injection bugs. Additionally, try to a void using dynamic column names unless you've very carefully screened them against a known-good list.foreach($pretag as &$indtag)by reference?echo $row['$indtag'];I think that need to be double qouted.