I’m trying to debug some auto-generated code, but I am a mySQL noob. Everything goes fine ultil the “prepare” line below, and then for some reason $mysqli_stmt is false, yielding the stated error. Could it have something to do with the SQL_MODE = ‘ANSI’? The failure seems to have something to do with the string ‘xxx’ below, but it still happens no matter what I change it to. This value is meant to be a default value for the TickerDigest field, but strangely if I change ‘xxx’ to ‘c_u_TickerDigest’, then it suddenly works, but the TickerDigest field is inserted as ‘null’ when I look in the database.
$mysqli = mysqli_init();
$mysqli->options(MYSQLI_INIT_COMMAND, "SET SQL_MODE = 'ANSI'");
$mysqli->real_connect(SR_Host,SR_Username,SR_Password,SR_Database) or die('Unable to connect to Database');
$sql_stmt = 'INSERT INTO "t_sr_u_Product"("c_u_Name", "c_u_Code", "c_u_TickerDigest") VALUES (?, ?, "xxx")';
$mysqli_stmt = $mysqli->prepare($sql_stmt);
Fatal error: Uncaught exception ‘Exception’ with message ‘INSERT INTO “t_sr_u_Product”(“c_u_Name”, “c_u_Code”, “c_u_TickerDigest”) VALUES (?, ?, “xxx”): prepare statement failed: Unknown column ‘xxx’ in ‘field list” in P:\StarRise\SandBox\GateKeeper\Rise\srIProduct.php on line 18
I’m hopeful what’s going wrong is fairly simple, since I’m almost completely ignorant about SQL.
Single quotes around the ‘xxx’ should work. In ANSI SQL double quotes are for identifiers(for instance field & tablenames). See: http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html#sqlmode_ansi_quotes Identifiers are explained at: http://dev.mysql.com/doc/refman/5.1/en/identifiers.html