List Info

Thread: note 74577 added to function.mysql-fetch-field




note 74577 added to function.mysql-fetch-field
user name
2007-04-18 00:43:51
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.
----
Server IP: 64.71.164.2
Probable Submitter: 24.117.184.248
----
Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-fie
ld.php
Edit        -- https://master
.php.net/note/edit/74577
Del: integrated  -- h
ttps://master.php.net/note/delete/74577/integrated
Del: useless     -- http
s://master.php.net/note/delete/74577/useless
Del: bad code    -- htt
ps://master.php.net/note/delete/74577/bad+code
Del: spam        -- https:/
/master.php.net/note/delete/74577/spam
Del: non-english -- 
https://master.php.net/note/delete/74577/non-english
Del: in docs     -- http
s://master.php.net/note/delete/74577/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/74577
Reject      -- https://mast
er.php.net/note/reject/74577
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )