1

I would like to insert 3 rows in table dictionary and another 3 rows in table banner, so in this example I left only 1 query for each table. the error I'm getting is

Object of class PDOStatement could not be converted to string

try {
$conn = new PDO('mysql:host=mysql;dbname=mydb;charset=utf8mb4', 'root', 'tiger');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();

$conn->exec("INSERT INTO `dictionary` (`id`, `it`, `en`, `fr`, `es`, `pt`, `de`, `nl`) VALUES ('1', 'Sed tempus libero a tristique placerat.\r\n', 'Curabitur at justo sit amet mi aliquam vestibulum.\r\n', 'Duis sed elit suscipit, venenatis ipsum vitae, molestie elit.\r\n', 'Ut ac tortor semper, finibus est ac, porta erat.\r\n', 'Morbi sit amet quam facilisis, tristique mi a, sagittis quam.\r\n', 'Nulla nec sem quis velit tristique tempus vel id augue.\r\n', 'Mauris tincidunt leo eget tincidunt bibendum.\r\n')");
echo "New dictionary created successfully";

$stmt = $conn->prepare("INSERT INTO `banner` (`id`, `dictionaryId`, `name`, `destinationURL`, `domains`, `imageURL`, `position`, `startDate`, `finishDate`) VALUES ('5', '1', 'blink', 'github.com', 'Github', :imageURL, '5', '2019-04-18 03:00:00', '2019-05-04 00:00:00')");
$stmt->bindValue(':imageURL', $dropboxLink);
$stmt->execute();
$conn->commit();
echo "New banner created successfully";
} catch (PDOException $e) {
echo $stmt . "<br>" . $e->getMessage();
}

$conn = null;
3
  • echo $stmt expects $stmt to be a string, or to be convertable to a string. Since it is a PDOStatement, it is not. Change it to: echo print_r($stmt, true). Commented Apr 20, 2019 at 10:31
  • @KIKOSoftware I commented echo in the catch error and still doesn't work Commented Apr 20, 2019 at 10:34
  • Just remember for your next question: Saying: "it doesn't work", gives very little information. Always tell us what the error messages are, and if they stay the same you should also tell us. Commented Apr 20, 2019 at 11:32

1 Answer 1

1

Here is the modified code, try this

try {
  $conn = new PDO('mysql:host=mysql;dbname=mydb;charset=utf8mb4', 'root', 'tiger');
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $conn->beginTransaction();

  $stmt1 = $conn->prepare("INSERT INTO `dictionary` (`id`, `it`, `en`, `fr`, `es`, `pt`, `de`, `nl`) VALUES ('1', 'Sed tempus libero a tristique placerat.\r\n', 'Curabitur at justo sit amet mi aliquam vestibulum.\r\n', 'Duis sed elit suscipit, venenatis ipsum vitae, molestie elit.\r\n', 'Ut ac tortor semper, finibus est ac, porta erat.\r\n', 'Morbi sit amet quam facilisis, tristique mi a, sagittis quam.\r\n', 'Nulla nec sem quis velit tristique tempus vel id augue.\r\n', 'Mauris tincidunt leo eget tincidunt bibendum.\r\n')");
  $stmt1->execute();
  echo "New dictionary created successfully";

  $stmt2 = $conn->prepare("INSERT INTO `banner` (`id`, `dictionaryId`, `name`, `destinationURL`, `domains`, `imageURL`, `position`, `startDate`, `finishDate`) VALUES ('5', '1', 'blink', 'github.com', 'Github', :imageURL, '5', '2019-04-18 03:00:00', '2019-05-04 00:00:00')");
  $stmt2->bindValue(':imageURL', $dropboxLink);
  $stmt2->execute();
  $conn->commit();
  echo "New banner created successfully";
} catch (PDOException $e) {
 echo $e->getMessage();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.