Accessing multiple tables within a database to compile information to send email

I could probably figure this out but it would likely be sloppy and an embarrassment. So maybe someone will help me…

Here is what I need to do (all in same database):

  1. Search table for userid
    $result = mysqli_query($db,“SELECT userid FROM TABLE1 where listid=‘6’”);

  2. Search another table for email based on userid
    $result = mysqli_query($db,“SELECT email FROM TABLE2 where id=”$userid"");

  3. Search another table for the users name
    $result = mysqli_query($db,“SELECT name FROM TABLE3 where id=”$userid"");

  4. Use $name & $email to send an email to each individual in the database

REPEAT until there are no additional values for query #1


You would use a single JOIN query.

The following should (untested) work -

$sql = "SELECT,
	FROM table1 t1
	JOIN table2 t2 ON
	JOIN table3 t3 ON
	WHERE t1.listid=?"; // the ? is a prepared query place-holder

You should use a prepared query to supply the listid value (6) to the query when it gets executed to prevent any sql special characters in a value from being able to break the sql query syntax, which is how sql injection is accomplished. If the mysqli extension seems overly complicated to use with prepared queries, now would be the time to switch to the much simpler and more modern PDO database extension.

Sponsor our Newsletter | Privacy Policy | Terms of Service