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!
Your are trying to execute the lastInsertId-function on a string object; Try this one:
echo $conn->lastInsertId();