Jackbeeby Jackbeeby - 1 year ago 68
MySQL Question

Putting another query inside the where clause mysql and php

What is the simplest way of putting another mysql query inside a where clause in php.

For example I have tried:

$sql = "SELECT app, description FROM all-apps WHERE app!='(SELECT user, app FROM users-apps WHERE user="$user_name")'";

I need to bring up all the apps entries in the database. and then check if the user has the same app name in his list. if so it will not show up.

Basically there are 2 databases one has all apps and the other one has apps which the users have used and I need the query to find all the apps the user has not used.

Please comment bellow if this question is up to standard. Please have an opinion so I can fix on my mistakes in the future.

Answer Source

A few changes needed

"SELECT app, description FROM all-apps WHERE app NOT IN (SELECT app FROM users-apps WHERE user='{$user_name}')";

Note that this and your original query may leave you vulnerable to sql injection. Please use PDO prepared statements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download