0

Actually result is coming from mysql_query(), it results sometimes array or sometimes associative array how to identify it.

Sodium
  • 1,016
  • 1
  • 9
  • 22
  • 4
    Why would you need that? – Madara's Ghost Sep 26 '14 at 18:57
  • When i enter query 'show databases' it reaults in assoc array and when 'select statement' it results in array , my problem is how to process result. – Sodium Sep 26 '14 at 19:00
  • Why are you trying to do it in the same function? Have one function to handle show databases, and one to handle SELECTs. – Madara's Ghost Sep 26 '14 at 19:01
  • Its fine but is there any alternative to handle it via same function – Sodium Sep 26 '14 at 19:03
  • http://php.net/manual/en/function.mysql-fetch-assoc.php and if you can .. use pdo as mysql methods are deprecated http://php.net/manual/en/pdostatement.fetch.php – zod Sep 26 '14 at 19:06
  • @GauravGenius there probably are. But do yourself a favor and handle it in two different functions. Future you will thank you for it. – Madara's Ghost Sep 26 '14 at 19:18
  • possible duplicate of [How to check if PHP array is associative or sequential?](http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential) – forivall Sep 27 '14 at 01:55

1 Answers1

2

You actually get an object from mysqli_query(), which can either be converted into an associative array with mysqli_fetch_assoc($mysqli_obj) or an array with mysqli_fetch_array($mysqli_obj).

But to determine if a variable is an associative array:

function isAssoc($obj) {
    if(is_object($obj)) {
        $array = get_object_vars($obj);
    }
    else {
        $array = $obj;
    }
    return (count(array_filter(array_keys($array), 'is_string'))) ? true : false;
}
Evadecaptcha
  • 1,403
  • 11
  • 17
  • My result is resource_id and array_keys, array_filter requires parameter as array, getting warning – Sodium Sep 26 '14 at 19:16
  • Because you're passing it an object, if you're passing the direct results of $results = mysqli_query(). Which is never an array. If you're trying to pass an object directly, I'll try to see if there's a way to accommodate that. – Evadecaptcha Sep 26 '14 at 19:20
  • I edited the function to allow it to accept objects. You can try it again. – Evadecaptcha Sep 26 '14 at 19:25
  • But I honestly don't ever see a need for this, since objects will never contain indices as their property names. – Evadecaptcha Sep 26 '14 at 19:30
  • So what you like to prefer for this requirement – Sodium Sep 26 '14 at 19:32
  • Exactly what do you need from the mysql result? Is it a select statement? You might be confusing the array thing. Because mysqli can also return false, if a select query doesn't find anything. Or if there is an sql error. You may need to tell me your actual sql query string for me to know exactly what you need here. – Evadecaptcha Sep 26 '14 at 19:40
  • It could be select statement or some mysql specific statement like show databases, or show results etc. – Sodium Sep 26 '14 at 19:43
  • In pretty much every case the return will be an object or a bool (false). – Evadecaptcha Sep 26 '14 at 19:48
  • This is how the list of tables would be accessed: if($results) { $tables = []; while($row = mysqli_fetch_assoc($results)) { foreach($row as $key => $value) { $tables[] = $value; } } } – Evadecaptcha Sep 26 '14 at 19:57
  • Thanks Evade Captcha – Sodium Sep 26 '14 at 20:05