Created
December 15, 2014 15:32
-
-
Save johnmorris/5bc0b892fe1fbecda4ac to your computer and use it in GitHub Desktop.
INSERT data into a MySQL database with PHP using PDO and MySQLi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Be sure to input YOUR database details below | |
// Watch the associated videos here: https://www.youtube.com/playlist?list=PLLs69n7Q4dCx5_7ZwnxTymH8X0iRP_2vw | |
$db_user = 'DB USERNAME HERE'; | |
$db_pass = 'DB PASSWORD HERE'; | |
$db_name = 'DB NAME HERE'; | |
$db_host = 'localhost'; | |
$time = time(); | |
$query = "INSERT INTO objects (post_title, post_content, post_name, post_date) | |
VALUES ('Test 3', 'Test 3 content', 'test_3', FROM_UNIXTIME({$time}))"; | |
// MySQLi | |
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name); | |
if ($mysqli->connect_errno) { | |
printf("Connect failed: %s\n", $mysqli->connect_error); | |
exit(); | |
} | |
$result = $mysqli->query($query); | |
print_r($result); | |
// PDO | |
//try { | |
// $conn = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass); | |
// $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
// $result = $conn->query($query); | |
// | |
// print_r($result); | |
//} catch(PDOException $e) { | |
// echo 'ERROR: ' . $e->getMessage(); | |
//} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment