From dc0c7f43811b6c9d6a02367500745397e9814b91 Mon Sep 17 00:00:00 2001 From: William Miceli Date: Sun, 24 Nov 2019 22:26:17 -0500 Subject: [PATCH] Using PHP's PDO syntax now --- var/www/phptest.php | 46 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/var/www/phptest.php b/var/www/phptest.php index cdf9f10..01d907d 100644 --- a/var/www/phptest.php +++ b/var/www/phptest.php @@ -1,38 +1,38 @@ connect_error) { - die("Connection failed: " . $conn->connect_error); -} -echo "Connected successfully"; - -?> +// Current user +$user = "William"; // Testing with until we have login working -

PHP connect to MySQL

+

PHP Test Page - Connecting to MySQL

'; + try{ + $connection = new PDO("mysql:host=$server;dbname=$database", $username, $password); + // set the PDO error mode to exception + $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + echo "Connected successfully"; } - //Step 4 + catch(PDOException $e){ + echo "Connection failed: " . $e->getMessage(); + } + + $sql = "SELECT messageID, date, message + FROM messages + WHERE recipient=$user"; + foreach($connection->query($sql) as $row){ + print $row['messageID'] . "\t"; + print $row['date'] . "\t"; + print $row['message'] . "\t"; + } + mysqli_close($db); ?>