note 42255 modified in function.sqlite-fetch-array by cece

From: Date: Wed, 27 Oct 2004 12:48:56 +0000
Subject: note 42255 modified in function.sqlite-fetch-array by cece
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79169@lists.php.net to get a copy of this message
[Editor's note: to get short column names there's an undocumented PRAGMA setting. You can exec "PRAGMA short_column_names = ON" to force that behavior.] I noticed that if you use Joins in SQL queries, the field name is messed up with the dot! for example if you have this query: SELECT n.*, m.nickname FROM news AS n, members AS m WHERE n.memberID = m.id; now if you want to print_r the results returned using SQLITE_ASSOC type, the result array is like this : array ( [n.memberID] => 2 [n.title] => test title [m.nickname] => NeverMind [tablename.fieldname] => value ) and I think it looks horriable to use the variable ,for example, $news['m.nickname'] I just don't like it! so I've made a small function that will remove the table name (or its Alias) and will return the array after its index is cleaned <?php function CleanName($array) { foreach ($array as $key => $value) { //if you want to keep the old element with its key remove the following line unset($array[$key]); //now we clean the key from the dot and tablename (alise) and set the new element $key = substr($key, strpos($key, '.')+1); $array[$key] = $value; } return $array; } ?> --was-- I noticed that if you use Joins in SQL queries, the field name is messed up with the dot! for example if you have this query: SELECT n.*, m.nickname FROM news AS n, members AS m WHERE n.memberID = m.id; now if you want to print_r the results returned using SQLITE_ASSOC type, the result array is like this : array ( [n.memberID] => 2 [n.title] => test title [m.nickname] => NeverMind [tablename.fieldname] => value ) and I think it looks horriable to use the variable ,for example, $news['m.nickname'] I just don't like it! so I've made a small function that will remove the table name (or its Alias) and will return the array after its index is cleaned <?php function CleanName($array) { foreach ($array as $key => $value) { //if you want to keep the old element with its key remove the following line unset($array[$key]); //now we clean the key from the dot and tablename (alise) and set the new element $key = substr($key, strpos($key, '.')+1); $array[$key] = $value; } return $array; } ?> http://php.net/manual/en/function.sqlite-fetch-array.php

« previous php.notes (#79169) next »