--- id: php-mysql-connect title: "PHP MySQL Connect" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["MySQLi vs PDO", "PHP MySQL ์—ฐ๊ฒฐ"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["php", "programming", "w3schools", "mysql", "mysqli", "pdo"] raw_sources: ["https://www.w3schools.com/php/php_mysql_connect.asp"] applied_in: [] github_commit: "" --- # [[PHP MySQL Connect]] ## ๐ŸŽฏ ํ•œ ์ค„ ํ†ต์ฐฐ (One-line insight) Switching database systems later is trivial with PDO (change the connection string + a few queries) but requires rewriting EVERYTHING with MySQLi (queries included) โ€” since PDO works across 12 different database systems while MySQLi only works with MySQL, the choice between them is really a bet on whether the project will ever need to migrate databases. [S1] ## ๐Ÿง  ํ•ต์‹ฌ ๊ฐœ๋… (Core concepts) - **MySQLi (MySQL Improved)** โ€” works only with MySQL; offers both object-oriented AND procedural APIs. [S1] - **PDO (PHP Data Objects)** โ€” works across 12 different database systems; object-oriented only. [S1] - **Both support Prepared Statements** โ€” critical for protecting against SQL injection. [S1] - **Connection requires 4 pieces** โ€” server name, username, password, database name. [S1] - **PDO's exception-based error handling** โ€” `PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION` โ€” throws exceptions on query problems, caught via `catch(PDOException $e)`. [S1] - **Closing connections** โ€” automatic at script end; can be forced early via `$conn->close()` (MySQLi OOP), `mysqli_close($conn)` (procedural), or `$conn = null;` (PDO). [S1] ## ๐Ÿ“– ์„ธ๋ถ€ ๋‚ด์šฉ (Details) - MySQLi OOP connection: `$conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }`. [S1] - MySQLi procedural connection: `$conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }`. [S1] - PDO connection: `try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); }`. [S1] ## โš–๏ธ ๋ชจ์ˆœ ๋ฐ ์—…๋ฐ์ดํŠธ (Contradictions & updates) - **DB ์ „ํ™˜ ์šฉ์ด์„ฑ์˜ ์ฐจ์ด**: PDO๋Š” ๋‹ค๋ฅธ DB๋กœ ์ „ํ™˜ ์‹œ ์—ฐ๊ฒฐ ๋ฌธ์ž์—ด๊ณผ ์ผ๋ถ€ ์ฟผ๋ฆฌ๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋˜์ง€๋งŒ, MySQLi๋Š” ์ฟผ๋ฆฌ๋ฅผ ํฌํ•จํ•œ ์ „์ฒด ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ด ๋ช…ํ™•ํžˆ ๋Œ€๋น„๋จ. [S1] ## ๐Ÿ› ๏ธ ์ ์šฉ ์‚ฌ๋ก€ (Applied in summary) ํ˜„์žฌ ๋ฐœ๊ฒฌ๋œ ์‹ค์ œ ์ ์šฉ ์‚ฌ๋ก€๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค โ€” PDO์˜ ์˜ˆ์™ธ ๊ธฐ๋ฐ˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ(ERRMODE_EXCEPTION)๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ๋ฌธ์ œ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๋‹ค๋ฃจ๋Š” ํ‘œ์ค€ ํŒจํ„ด์ด๋‹ค. [S1] ## ๐Ÿ’ป ์ฝ”๋“œ ํŒจํ„ด (Code patterns) PDO connection with exception-based error handling (PHP): ```php try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ``` ## โœ… ๊ฒ€์ฆ ์ƒํƒœ ๋ฐ ์‹ ๋ขฐ๋„ - **์ƒํƒœ:** draft - **๊ฒ€์ฆ ๋‹จ๊ณ„:** conceptual - **์ถœ์ฒ˜ ์‹ ๋ขฐ๋„:** B (W3Schools โ€” widely used educational reference, not a primary standards body) - **์‹ ๋ขฐ ์ ์ˆ˜:** 0.90 - **์ค‘๋ณต ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ:** ์‹ ๊ทœ ์ƒ์„ฑ (New discovery) ## ๐Ÿ”— ์ง€์‹ ๊ทธ๋ž˜ํ”„ (Knowledge Graph) - **์ƒ์œ„/๋ฃจํŠธ:** [[PHP Tutorial]] - **๊ด€๋ จ ๊ฐœ๋…:** [[PHP MySQL Intro]], [[PHP MySQL Create DB]], [[PHP Exception]] - **์ฐธ์กฐ ๋งฅ๋ฝ:** MySQLi/PDO ์—ฐ๊ฒฐ ๋ฐฉ์‹ โ€” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ(Create DB) ์ฑ•ํ„ฐ๋กœ ์ด์–ด์ง. ## ๐Ÿ“š ์ถœ์ฒ˜ (Sources) - [S1] W3Schools โ€” PHP Connect to MySQL โ€” https://www.w3schools.com/php/php_mysql_connect.asp ## ๐Ÿ“ ๋ณ€๊ฒฝ ์ด๋ ฅ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP Connect to MySQL" page (Astra wiki-curation, P-Reinforce v3.1 format).