Although they both worked fine in Apache in which I coded them, yet here in OLS I have tried both PDO and mySQLi INSERT statements, . . . and neither one work.
All I ever get is a PHP response that says, "[object Object]".
Now I know that my database connection is good because my preceding SELECT statements do work.
PDO:
$stmt = $db->prepare("INSERT INTO table1(name, date) VALUES ( :first_name, :time_stamp)");
$stmt->bindParam(':first_name' => $first_name);
$stmt->bindParam(':time_stamp' => $time_stamp);
$stmt->execute();
mySQLi:
$db = new mysqli('localhost', 'root', 'xxx[password removed]xxxx', 'users');
if ($db->connect_error) {
if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
$stmt = $db->prepare("INSERT INTO table1(name, date) VALUES (?, ?)");
$stmt->bind_param("si", $first_name, $time_stamp); // s = string; i = integer
$stmt->execute();
I'm running OLS on the cheapest DigitalOcean Linux webserver droplet; however, this program was coded using Apache on a Windows 10 System. I'm also using php 5 (lsphp5), phpMyAdmin, and MariaDB. I also tried OLS's out-of-box php 7 (lsphp).
All I ever get is a PHP response that says, "[object Object]".
Now I know that my database connection is good because my preceding SELECT statements do work.
e.g.
$stmt = $db->prepare("SELECT date FROM table1");
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_COLUMN);
//worked!
Here below are the two ways that I tried it, both of which failed:$stmt = $db->prepare("SELECT date FROM table1");
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_COLUMN);
//worked!
PDO:
try{
$db = new PDO('mysql:host=localhost; dbname=users; charset=utf8', 'root', 'xxx[password removed]xxxx');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo $e->getMessage();
die();
}
$db = new PDO('mysql:host=localhost; dbname=users; charset=utf8', 'root', 'xxx[password removed]xxxx');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo $e->getMessage();
die();
}
$stmt = $db->prepare("INSERT INTO table1(name, date) VALUES ( :first_name, :time_stamp)");
// The ": parameter" syntax is just a placeholder that gets filled in with the values below.
$stmt->bindParam(':time_stamp' => $time_stamp);
$stmt->execute();
mySQLi:
$db = new mysqli('localhost', 'root', 'xxx[password removed]xxxx', 'users');
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
} if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
$stmt = $db->prepare("INSERT INTO table1(name, date) VALUES (?, ?)");
$stmt->bind_param("si", $first_name, $time_stamp); // s = string; i = integer
$stmt->execute();
I'm running OLS on the cheapest DigitalOcean Linux webserver droplet; however, this program was coded using Apache on a Windows 10 System. I'm also using php 5 (lsphp5), phpMyAdmin, and MariaDB. I also tried OLS's out-of-box php 7 (lsphp).
Last edited: