note 74849 added to function.mysql-query
| From: | earlsinclair2001 at yahoo dot com | Date: | Mon, 30 Apr 2007 20:14:22 +0000 |
| Subject: | note 74849 added to function.mysql-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125995@lists.php.net to get a copy of this message | ||
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 -- https://master.php.net/note/delete/74849/integrated
Del: useless -- https://master.php.net/note/delete/74849/useless
Del: bad code -- https://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 -- https://master.php.net/note/delete/74849/in+docs
Del: other reasons-- https://master.php.net/note/delete/74849
Reject -- https://master.php.net/note/reject/74849
Search -- https://master.php.net/manage/user-notes.php