-1

I have this select statement in my php application:

$result = mysqli_query($con,"SELECT * FROM apps WHERE device.DeviceName = $os ");

I'm trying to select all "apps" that have the device name of the variable "$os" all the apps are in one table with a device id, and the device table holds the device name...

Carla Dessi
  • 9,086
  • 9
  • 39
  • 53
  • Even though you are using mysqli, you are still vulnerable to SQL injection. See [How can I prevent SQL injection in PHP?](http://stackoverflow.com/q/60174) – Madara's Ghost Mar 15 '14 at 10:37

2 Answers2

0

Join your device table, otherwise you can't use it in the WHERE clause

SELECT *
FROM apps A
    INNER JOIN device D
        ON D.id = A.deviceID
WHERE D.DeviceName = '$os'

As the comments suggests, this is vulnerable to injection though, so be careful.

domdomcodecode
  • 2,355
  • 4
  • 19
  • 27
0

You are looking for JOIN. Take a look at this article for some more help.

Essentially a JOIN sticks two tables together (you can have many joins) based on the criteria you specify. The most often used criteria is the foreign key.

The join you would be looking for is this:

SELECT * FROM apps 
INNER JOIN device ON apps.deviceID = device.id 
WHERE device.DeviceName = $os

Also, you are open to SQL injection vulnerabilities by putting the parameter directly in the query. Read this great stack overflow question.

Community
  • 1
  • 1
SamV
  • 7,548
  • 4
  • 39
  • 50