note 39332 added to function.msql-fetch-array
| From: | daveatddhwebdotcom at rn2 dot php dot net | Date: | Sun, 25 Jan 2004 07:25:07 +0000 |
| Subject: | note 39332 added to function.msql-fetch-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64128@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.msql-fetch-array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39332
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39332&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39332&report=yes
Search -- http://master.php.net/manage/user-notes.php