Tuesday, 28 August 2018

PHP array does not save values ​​as I expected


Today I was recoding an old program in PHP and I used arrays to organize it better. After coding it, when I tested it I realized that the arrays were not saving the values as I expected them to do. (I'm kind of a newbie in PHP)


Here is how I defined the arrays:
/* Mysql settings */
$mysql = array();

$mysql['track'] = array();
$mysql['track']['visitors'] = array();
$mysql['track']['visitors']['query'] = "SELECT `uid`, `referer` FROM `track` WHERE (`time` >= '".$time['request']['start']."' AND `time` < '".$time['request']['end']."') GROUP BY `uid`";

I used var_dum to see how the values where saved and made an mysql request:
var_dump($mysql['track']);
...
mysql_query($mysql['track']['visitors']['query']);

Here is the output:
array(1) {
  ["query"]=>
  string(71) "SELECT `request` FROM `track` WHERE (`time` >= '' AND `time` < '86400')"
}
Query was empty

Can someone tell me why does this happen? I'm I using arrays in a wrong way? How should I use them to this purpose?

Your query is being stored in the ['query'] array according to var dump, but your mysql_query is using ["track"]. Try switching your query to ['query'] array.
Also please start using mysqli instead of mysql since the latter is deprecated. This is old code as you say, so I'm not sure if you were aware, but I thought I'd say it at least.

0 comments:

Post a Comment