2

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?

6
  • 3
    WARNING: When using mysqli you should be using parameterized queries and bind_param to 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. Commented Jan 16, 2015 at 17:49
  • Any particular reason why foreach($pretag as &$indtag) by reference? Commented Jan 16, 2015 at 17:51
  • 1
    I'm actually fairly alarmed that you have a table named beyonce... Commented Jan 16, 2015 at 17:51
  • 2
    I'm not sure this is your whole problem, but I don't think this line would work. echo $row['$indtag']; I think that need to be double qouted. Commented Jan 16, 2015 at 18:06
  • 1
    BOOOOOOOMM thank you Broken, that worked. Love you. Commented Jan 16, 2015 at 18:39

2 Answers 2

2

You're using the variable $indtag where you should be using the column name indtag:

SELECT `id`, `$indtag` FROM `beyonce` WHERE `$indtag` LIKE '%$indtag%'
              ^                              ^

And as @tadman points outs, don't do it this way, use mysqli_stmt_bind_param with a prepared statement or you are in for a wild ride.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm pretty sure the column being selected is not indtag but is actually dynamically changing. Based on the question, the value of $indtag could be 'ang', 'rom', 'dece', ... etc.
I'm specifically referring to this line: The table has fields for ids, quotes, then an individual column for each tag (ang for anger being an example).
BrokenBinary is correct in this, it is a dynamically changing column.
So this is a valid example: SELECT id, rom FROM beyonce WHERE rom LIKE '%rom%' ???
OK, that is a horrendous design, but look at @BrokenBinary's comment on the OP.
|
0

This line in your code doesn't need quotes.

echo $row['$indtag'];  // Won't work
echo $row[$indtag];    // Will work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.