开发者

Why can I not access a field of my mysqli result with $row['price'+$i+'dollar'] with PHP?

In a database I'm working with, there are 6 columns with names:

price1dollar
price2dollar
price3dollar
price4dollar
price5dollar
price6dollar

I thought that onc开发者_开发百科e I had the result set for the query, I could access it like so with PHP:

for($i = 1; $i <= 6; $i++) {
    echo $row['price'+$i+'dollar'].'<br />';
}

and that would print out all the values, but I'm not getting anything printed.

if I use:

echo $row['price1dollar'];

then the value is printed out fine.

I have echoed the value of $i and it prints out 1 to 6 fine, so I don't know whats going on.

Is the $row['price'+$i+'dollar'] syntax not allowed?

Many thanks


should be

$row['price' . $i . 'dollar']

in PHP there are . (dot) string connections (+ (plus) is i think for example in Java)


In PHP, the + symbol is the addition operator and it doesn't make sense to perform arithmetic operations on strings. It won't trigger a syntax error since PHP will try to convert your strings into numbers (e.g. 'price' becomes 0) but it won't render anything useful.

You probably want the . symbol which corresponds to the concatenation operator.

See also the var_dump() function, which is very useful to find out what your data contains.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜