Shazer2 Posted December 15, 2011 Share Posted December 15, 2011 I am designing my own framework, and I have created a select function. I have set a parameter to be an array by default, with the default value of *. When I check if it's an array (with is_array) it says that it isn't. Does anyone have any idea why it would say that? function select($table, $rows = array('*'), $cond = "") { $query = "SELECT {$rows} FROM `{$table}`"; if (isset($cond)) { $query .= " WHERE {$cond}"; } if (is_array($rows)) { if (isset($rows) && $rows != "*") { foreach ($rows as $v) { print_r($v); } } } //return mysql_query($query); } Any help is appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/253200-select-function/ Share on other sites More sharing options...
Pikachu2000 Posted December 15, 2011 Share Posted December 15, 2011 I don't see anything in the function that would even return an error telling you it isn't an array. What makes you think that's the case here? Quote Link to comment https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298021 Share on other sites More sharing options...
Shazer2 Posted December 15, 2011 Author Share Posted December 15, 2011 Well I had this code before: $check = is_array($rows) ? "Yes" : "No"; echo $check; It responded with no. Quote Link to comment https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298022 Share on other sites More sharing options...
Pikachu2000 Posted December 15, 2011 Share Posted December 15, 2011 I just added some quick debugging (see comments in the code), and it seems fine to me. function select($table, $rows = array('*'), $cond = "") { $query = "SELECT {$rows} FROM `{$table}`"; if (isset($cond)) { $query .= " WHERE {$cond}"; } if (is_array($rows)) { return "is array"; // <<<---- HERE if (isset($rows) && $rows != "*") { foreach ($rows as $v) { print_r($v); } } } else { return "not array"; // <<<--- AND HERE } } echo select(); // returns "is array" Quote Link to comment https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298023 Share on other sites More sharing options...
Shazer2 Posted December 15, 2011 Author Share Posted December 15, 2011 Alright, I copied that code directly and then done: echo $db->select("boot", "id", "id=1"); The output is "not array". Quote Link to comment https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298024 Share on other sites More sharing options...
Pikachu2000 Posted December 15, 2011 Share Posted December 15, 2011 Well of course it won't be an array when you explicitly pass a string; any value you pass to the function will override the default value. You'd need to pass the argument as an array. echo select("boot", array("id"), "id=1"); You're also going to need to modify the function to implode that array with commas to make it usable in the query string. Quote Link to comment https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298025 Share on other sites More sharing options...
Shazer2 Posted December 15, 2011 Author Share Posted December 15, 2011 I have no idea how I missed that, thanks heaps. Yeah, I will, I wasn't up to that yet though. Quote Link to comment https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298026 Share on other sites More sharing options...
kicken Posted December 15, 2011 Share Posted December 15, 2011 Just as a quick side note about your code: if (isset($cond)) { $query .= " WHERE {$cond}"; } isset() is probably not what you want. isset will be true pretty much always for a parameter, unless it's value is null. If you want to check that it's got a value that is not the empty string, use !empty() or just if ($var). if (is_array($rows)) { if (isset($rows) && $rows != "*") { Checking isset() is a bit pointless there. if it is an array, it is set. Also, as mentioned above isset would only return false if you pass null, not if you pass say an empty string or empty array. Also, if $rows is an array, then it is guaranteed to != '*'. Perhaps you ment $rows[0] != '*'? function select($table, $rows = array('*'), $cond = "") { $query = "SELECT {$rows} FROM `{$table}`"; if ($cond){ $query .= " WHERE {$cond}"; } if (is_array($rows) && $rows[0] != '*') { foreach ($rows as $v) { print_r($v); } } //return mysql_query($query); } Quote Link to comment https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298060 Share on other sites More sharing options...
Shazer2 Posted December 15, 2011 Author Share Posted December 15, 2011 I fixed it all up. Thanks anyway for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298063 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.