0

I have some queries with inner joins and I have some give attributes such as this case:

...INNER JOIN users ON ? = users.id
                WHERE report_cases_accepted.report_id=?

now the question is, is it correct if I put that "?" after ON?

showtime
  • 1
  • 1
  • 17
  • 48
  • 2
    I'd expect a column from the other table in the ON clause. – jarlh May 29 '20 at 12:45
  • 1
    Prepared statements accept only values as parameters hence you cannot represent a column name with `?` mark – Your Common Sense May 29 '20 at 12:47
  • @YourCommonSense I wasn't using prepared statements before so I was doing it like this `INNER JOIN users ON '$device_manufacturer' = users.id` and it returns what it should, so why is it working like this then? – showtime May 29 '20 at 12:50
  • wait. you cannot join a table on a condition that doesn't belong to that table. Something wrong with your join. You better ask another question about your SQL query – Your Common Sense May 29 '20 at 12:51
  • 1
    Are you trying to use a *value* in that first placeholder? If so, that sounds like a misuse of a `JOIN` that should be part of a `WHERE`. Or are you trying to dynamically use a *column* in that placeholder, such as having multiple columns in your table which have FK to `users`? There could be a variety of solutions to that logic, and the table design may be wrong to begin with. You *can* safely concatenate strings for that purpose, as long as those strings are **not** user-editable. For example, an array of known values in the code from which you select one based on your logic. – David May 29 '20 at 12:51
  • @David this variable `$device_manufacturer` has an id of a user, but how can I use in the Where clause since I dont know how to then join users table – showtime May 29 '20 at 12:57
  • @ttt: We don't know, since we know nothing of your table structure or what your query is attempting to select from that structure. But joining is done on *columns*, not on *values*. – David May 29 '20 at 12:58

0 Answers0