串联中的附加项无法按预期工作 [英] Concatenation with addition in it doesn't work as expected

查看:62
本文介绍了串联中的附加项无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的带有SQL查询的PHP代码,但是输出与预期不符:

Here is my PHP code with SQL query, but the output isn't as expected:

$sql = 'INSERT INTO `event_footers` (`event_id`, `order`, `file_id`, `url`) VALUES ';
foreach($all_footers as $key => $val){
    $sql .= '('.(int)$data['event_id'].', '.$key + 1 .', '.(int)$val['file_id'].', "'.addslashes($val['url']).'"), ';
}

$sql = rtrim($sql, ', ');
var_dump($sql);
exit;

然后我得到这样的sql查询:

AND I get sql query like this:

`INSERT INTO `event_footers` (`event_id`, `order`, `file_id`, `url`) VALUES 1, 2135, "http://11.lt"), 1, 2136, "http://22.lt"), 1, 2140, "http://44.lt")`

在VALUES之后的第一个(在哪里?

Where is the first ( after VALUES?

推荐答案

+.具有相同的

+ and . have the same operator precedence, but are left associative. Means after the first concatenation:

'(' . (int)$data['event_id']

该字符串已与您的密钥添加在一起,例如

The string got added with your key, e.g.

"($data['event_id']" + $key

因此,字符串被转换为整数在这种数字背景下消失.要解决此问题,请在您的加法周围加上括号().

So the string gets converted into an integer in that numerical context and disappears. To solve this use parentheses () around your addition.

这篇关于串联中的附加项无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆