1

I am a beginner programmer and am making a pretty simple insert into database application.

Here is my code:

$hostname = 'localhost';
$username = '***********';
$password = '**********';
$conn = new PDO("mysql:host=$hostname;dbname=***********", $username, $password);
$sql = ("INSERT INTO ******** (name, date_entered) VALUES (?, ?)");
$q = $conn->prepare($sql);
$q->execute(array($name, date("Y-m-d")));

var_dump($q); // Trying to figure out what the issue was
echo $sql->lastInsertId(); 

The insert works fine, but I can't get any value of lastInsertId. Why? The results of var_dump($q):

object(PDOStatement)#4662 (1) { ["queryString"]=> string(54) "INSERT INTO ******** (name, date_entered) VALUES (?, ?)" }

Thanks for any and all help! Its greatly appreciated!

Muhambi
  • 3,472
  • 6
  • 31
  • 55
  • Turn on error_reporting, because you would be getting something like `Trying to get property on a non-object at line x` `error_reporting(E_ALL); ini_set('display_errors', 1);` – Michael Berkowski Dec 24 '12 at 01:03

1 Answers1

3

Your are trying to execute the lastInsertId-function on a string object; Try this one:

echo $conn->lastInsertId(); 
Phil Rykoff
  • 11,999
  • 3
  • 39
  • 63