Display data from two MySQL database tables This is my first attempt at PHP PDO. I needed to get data from two tables in my MySQL database. I'm pleased to say the code works, and I was just wondering if what I've done is correct.

&lt;?php

// MySQL Database Connection
$db = new PDO('mysql:host=localhost;dbname=database;charset=utf8mb4', 'root', '');
$db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

// Display Data
foreach($db-&gt;query('SELECT * FROM data1 LEFT JOIN data2 ON data1.id = data2.id WHERE data1.id=1') as $row) {
    // Data is displayed in their respected places.
}

?&gt;
&lt;!doctype html&gt;

&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;

&lt;title&gt;Title&lt;/title&gt;
&lt;meta name="description" content="Description"&gt;
&lt;meta name="author" content="Author"&gt;

&lt;link rel="stylesheet" href="css/styles.css"&gt;

&lt;!--[if lt IE 9]&gt;
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"&gt;&lt;/script&gt;
&lt;![endif]--&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;table border="1"&gt;
    &lt;tr&gt;
        &lt;td&gt;&lt;?php echo $row['sample1']; ?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php echo $row['sample2']; ?&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;&lt;?php echo $row['extra1']; ?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php echo $row['extra2']; ?&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
foreach
Don't have an empty foreach loop... ever. Similarly, just because php lets $row escape from the context it is in does not mean you should use it after the foreach-loop is over.
PDO::query
I recommend against using PDO::query, especially because you lack a thorough understanding of what you are doing. You can use it when executing complete static queries (those that do not have variables), but you already have a where-clause in that sql statement and it is only a matter of time until you want to change that to something dynamic. Use PDO::prepare instead.
PDO::prepare
You are using $db-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false);. Good. Except that you do not use prepared statements at all. Keep in mind that your mysql driver must be able to use native prepared statements too.
PDO::ERRMODE_EXCEPTION
You are setting the error mode to PDO::ERRMODE_EXCEPTION. Good. Except that you do not catch any of them and instead let the page return blank.