I'm trying to insert values using prepared statements like this:
$dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO user_table (first_name, last_name) VALUES (:tname, :tname2)"; $stmt = $dbh->prepare($sql); $stmt -> bindParam(':tname', 'John'); $stmt -> bindParam(':tname2', 'Smith'); $stmt -> execute(); However, this is throwing a fatal error: "PHP Fatal error: Cannot pass parameter 2 by reference in /Applications/MAMP/htdocs/live/test_create.php on line 53" This is referring to this line: $stmt -> bindParam(':tname', 'John');
What's causing this problem?
$stmt -> bindParam(':tname', 'John'); $stmt -> bindParam(':tname2', 'Smith'); $stmt -> execute()I see that, a bunch of spaces after the$stmtvariable.'John'or somewhere else? I tried the code above as you have it and that yielded the same error.