note 74577 deleted from function.mysql-fetch-field by tularis
| From: | tularis@php.net | Date: | Wed, 18 Apr 2007 16:13:19 +0000 |
| Subject: | note 74577 deleted from function.mysql-fetch-field by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125553@lists.php.net to get a copy of this message | ||
Note Submitter: earlsinclair2001 at yahoo dot com
Reason: bad code
----
I was working on a new project where I had to constantly retrieve data from registered users on a
table called users. Often times I would need to retrieve data for a single user. I got tired of
writing the same code over and over so I decided to create a function to do that and I thought I
should share with people so if you have a similar problem you could use this function. Basically
what this function does is it retrieves data for a single row from the database and returns it in
the format of an array.
<?php
/************************************************
Arguments for the function:
$fieldsarray:
Array with the name of the fields to be included in the query
$table:
Name of the table where the query is going to be performed
$unique:
Name of the field that uniquely identifies the record (usually primary key)
$uniquevalue:
Value of the unique field for the desired record
************************************************/
function selectonerow($fieldsarray, $table, $unique, $uniquevalue)
{
//If value of $fieldsarray equals "*", use that value
//Otherwise, turn $fieldsarray into a string
if ($fieldsarray == "*")
{
$fields = $fieldsarray;
}
else
{
$fields = "";
foreach($fieldsarray as $currentfield)
{
$fields = $fields . $currentfield . ", ";
}
//removes the trailing ", " from the end of the string
$fieldlen = strlen($fields) - 2;
$fields = substr($fields, 0, $fieldlen);
}
//performs the query
$result = mysql_query("SELECT $fields FROM $table WHERE $unique LIKE
'$uniquevalue'") or die("Could not perform query - " . mysql_error());
$num_rows = mysql_num_rows($result);
//if there's no such record with $uniquevalue id, returns NULL
//otherwise create an array with the results
if($num_rows == NULL)
{
return NULL;
}
else
{
$queryresult = array();
$num_fields = mysql_num_fields($result);
$i = 0;
while ($i < $num_fields)
{
$meta = mysql_fetch_field($result, $i);
$queryresult[$meta->name] = mysql_result($result, 0, $meta->name);
$i++;
}
return $queryresult;
}
}
?>
Let's say you have a table 'users' with these fields: user_id, username, firstname,
lastname, email, where user_id is the primary key. So if you want to perform a query for, let's
say, user with user_id = 6, this is what you would need to do:
<?php
/*code to connect to the database*/
$userdata = selectonerow("*", "users", "user_id", "6");
?>
If only the fields 'firstname' and 'lastname' were desired to retrieve, this is
what should be done:
<?php
/*code to connect to the database*/
$fields = array("firstname", "lastname");
$userdata = selectonerow($fields, "users", "user_id", "6");
?>
Hope this can be useful to somebody.