Okay, this is probably something very stupid, but I get an error on this line:
saying that the [ is invalid. I can't see why...Code:$id = (int)$link->query("SELECT id FROM keys WHERE `key`='{$key}' LIMIT 1")[0]['id'];
Printable View
Okay, this is probably something very stupid, but I get an error on this line:
saying that the [ is invalid. I can't see why...Code:$id = (int)$link->query("SELECT id FROM keys WHERE `key`='{$key}' LIMIT 1")[0]['id'];
you can't access the array created by your function directly (the way you're trying to). PHP will allow you to access objects in this way, though, if you could rework your code to work like that (assuming it's your own class that has the query function). instead, you'd need:
PHP Code:$query = $link->query( ... );
$id = (int) $query[0]['id'];
Thanks!
Why is that, exactly?
My guess would be it's just a limit of the language's syntax, but it might have to do with the array not yet existing (or something like that).
I run into this issue at times, and if I can, I make those functions cast the array they're returning as an object. This won't help in numeric indexed arrays, but for associative arrays it works great:
PHP Code:<?php
$a = array("test" => "Test!", 0 => "Test 2!");
$b = (object) $a;
echo $b->test; // prints Test!
echo $b->0; // will throw an error
// syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$'
echo $b->{0}; // also throws an error
// Undefined property: stdClass::$0
?>
<pre><?php print_r($b); ?></pre>