开发者

The problem with the query

I can not find an answer to why this query does not work:

$query = pg_query($connect, sprintf("INSERT INTO table (uid, urzid, time_开发者_高级运维start, time_stop2, date_start, date_stop, cycle) VALUES ($uid, $pom, '$time1', '$time2', '$data1', '$data2', $cykl)"));

I get a warning: PG_QUERY() [FUNCTION.PG-QUERY]: QUERY FAILED: ERROR: SYNTAX ERROR AT OR NEAR "," AT CHARACTER 96

It seems to me that the error is associated with.'' $ time1, $ Time2 and $ date1, $ date2 are in accordance with the format for date and time.


You have to provide a format to the function sprintf

See the doc

And moreover, there's no needs at all of sprintf in this case.


We cannot guess what content is in your variables. Please run this code and paste the result:

echo "INSERT INTO table (uid, urzid, time_start, time_stop2, date_start, date_stop, cycle) VALUES ($uid, $pom, '$time1', '$time2', '$data1', '$data2', $cykl)";

You will find the syntax error.

The best way to avoid this kind of errors (and, even more importantly, to prevent SQL injection), is to use a library such as PDO and to use placeholders for variables.

Update: now that we could see what your variables look like, try this:

$query = pg_query($connect, "INSERT INTO table (uid, urzid, time_start, time_stop2, date_start, date_stop, cycle) VALUES ('$uid', '$pom', '$time1', '$time2', '$data1', '$data2', '$cykl')");

Don't forget to have a look at the above comment for best practices, however.


You have to add extra parameters :

$world = "World";
$foo = sprintf("Hello %s", $world);

However, as you are in a double quoted string you don't need sprintf :

$foo = "Hello $world";


Try reversing quoting, " => ', " => ' ... saves me a lot pain always.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜