List Info

Thread: note 74849 added to function.mysql-query




note 74849 added to function.mysql-query
user name
2007-04-30 15:14:22
On my latest project, very often, I needed to select a
unique row from the database. For example: a certain user
with certain username, or a row where the ID (primary key)
is X. I got tired of typing these queries over and over so I
created a simple function that will do just that: select one
row from the database where certain field is unique. I hope
this can be helpful to somebody:

<?php

function selectonerow($fieldsarray, $table, $uniquefield,
$uniquevalue)
{
	//The required fields can be passed as an array with the
field names or as a comma separated value string
	if(is_array($fieldsarray))
	{
		$fields = implode(", ", $fieldsarray);
	}
	else
	{
		$fields = $fieldsarray;
	}
	
	//performs the query
	$result = mysql_query("SELECT $fields FROM $table
WHERE $uniquefield = '$uniquevalue'") or
die("Could not perform select query - " .
mysql_error());
	
	$num_rows = mysql_num_rows($result);
		
	//if query result is empty, returns NULL, otherwise,
returns an array containing the selected fields and their
values
	if($num_rows == NULL)
	{
		return NULL;
	}
	else
	{
		$queryresult = array();
		$num_fields = mysql_num_fields($result);
		$i = 0;
		while ($i < $num_fields)
		{
			$currfield = mysql_fetch_field($result, $i);
			$queryresult[$currfield->name] = mysql_result($result,
0, $currfield->name);
			$i++;
		}
		return $queryresult;
	}
}

?>

This function assumes there is a MySQL connection already
established and the database to be used already selected.

Here is an example of usage:

selectonerow(fields, table name, unique field name, unique
field value)

Let's say I have a users table with the fields userid,
username, firstname, lastname and email. userid is the
primary key and username is a unique field. If you want to
select the firstname, lastname and email from the table
where the userid is 4:

<?php

$fields = array("firstname", "lastname",
"email");
$userdata = selectonerow($fields, "users",
"userid", 4);

?>

or

<?php

$userdata = selectonerow("firstname, lastname,
email", "users", "userid", 4);

?>

This will return an array to $userdata with the keys being
the field name and their respective value. This is how you
would print out their first name, last name and email, for
example:

<?php

echo $userdata['firstname'] $userdata['lastname']
$userdata['email'];

?>
----
Server IP: 69.39.81.135
Probable Submitter: 70.150.142.226
----
Manual Page -- http://www.php.net/manual/en/function.mysql-query.php
Edit        -- https://master
.php.net/note/edit/74849
Del: integrated  -- h
ttps://master.php.net/note/delete/74849/integrated
Del: useless     -- http
s://master.php.net/note/delete/74849/useless
Del: bad code    -- htt
ps://master.php.net/note/delete/74849/bad+code
Del: spam        -- https:/
/master.php.net/note/delete/74849/spam
Del: non-english -- 
https://master.php.net/note/delete/74849/non-english
Del: in docs     -- http
s://master.php.net/note/delete/74849/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/74849
Reject      -- https://mast
er.php.net/note/reject/74849
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 )