note 39332 deleted from function.msql-fetch-array by didou
| From: | didou@php.net | Date: | Mon, 04 Apr 2005 22:10:13 +0000 |
| Subject: | note 39332 deleted from function.msql-fetch-array by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-87465@lists.php.net to get a copy of this message | ||
Note Submitter: dave at ddh web dot com
----
As a supplementary note, the 'extract' feature should be used with caution, because it
imports the values into the 'current symbol table (see documentation on extract)
This can create a problem if any of the field names you are extracting correspond to names of
session variables. For instance, I would query the database for my 'member' table, which
contains a field 'mem_type'...which is also the name of my session variable, called with
$_SESSION['mem_type']...Whenever I would use the extract() function on this query,
whatever came back for 'mem_type' would then be substituted into my session variable
mem_type. This stumped me for about an hour, until I finally isolated it.
The best solution is to write a function such as:
function db_Select($input_query)
{
#returns an array of results based on query input
#query the database
$result = mysql_query($input_query) or die("Query failed : " . mysql_error());
#parse results into an array using mysql_fetch_array()
$array = mysql_fetch_array($result);
#free memory
mysql_free_result($result);
#return parsed array
return $array;
}
You can place this function into the library of functions that you include or require for your
database-related pages.
Then you call the function like this:
$sqlResults = db_Select("SELECT * FROM table_name")
Now You can call any field from the table_name table with $sqlResults['fieldname']...no
extract() required
The extract function is not good for this use. I don't think it was designed for that and it
certainly hung me up something fierce. Hope this helps.