Jump to content

PHP Conn

From Drywall Wiki
Revision as of 14:18, 7 January 2026 by Jlebeau81 (talk | contribs) (Created page with "//PHP PDO Connection to MySQL <$php $servername = "localhost"; // Server Name $username = "root"; //mysql username $password = "password"; //Database Password $dbname = "dbname"; //Database Name try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); //set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt =...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

//PHP PDO Connection to MySQL

<$php $servername = "localhost"; // Server Name $username = "root"; //mysql username $password = "password"; //Database Password $dbname = "dbname"; //Database Name

try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); //set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare("SELECT Roll_No, Name, City FROM 'Student Details'"); $stmt->execute();

//set the resulting array to associative $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo "

    "; foreach ($result as $row) { echo "
  • Roll No: " . $row["Roll_No"] . " - Name: " . $row["Name"] . " | City: " . $row["City"] . "
  • "; } echo "

";

} catch(PDOException $e) { echo "Error: " . $e->getMessage(); }

$conn = null; // close connection ?>