How To Correctly Specify A Default Value For A String Field In A PHP/MySQL Prepared Statement

Posted by Joshua on Stack Overflow See other posts from Stack Overflow or by Joshua
Published on 2010-06-17T18:42:59Z Indexed on 2010/06/17 19:03 UTC
Read the original article Hit count: 208

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql