VisageHosting.com
 
Home
 
Resources
- PHP and MySQL (2)
- HTML and CSS (0)
- CGI and Perl (0)
- Java (0)
- JavaScript (1)
- Python (0)
- ASP (1)
- Web Hosting/Servers (14)
 
Articles
- PHP/MySQL (11)
- HTML/CSS (5)
- Other Languages (4)
- Web Hosting (2)
- Computer Tech (7)
 
Forum
 
 
Register
Login

Username:

Password:

 
 
Currently 0 registered users and 4 guests browsing.

29 articles available for viewing.

1313398 total page hits.

Welcome to our newest registered user, choonchan!


PHP/MySQL -> Code Snippets



How to Grab a Single Field From a MySQL Table using PHP

- by surreal on 05/20/06, 3:35AM CEST (3314 Views)



If you ever need to grab the content of a single field from a MySQL table, you may find this function very useful.

First I will show you the code, and then explain what it does and how it can be used.

Here is the code:


Code:
function getField($field, $table, $method, $str) {

       // The query
       $query = "select `$field` from `$table` where `$method`='$str'";

       // Look up query
       $result = mysql_query($query);

       // Turn query into array
       $row = mysql_fetch_array($result);

       return $row["$field"];

}


The function will basically take what you input and turn it into a MySQL query. The results of the query are then turned into an array, with the one field you selected being returned at the end. This meas that the function getField() will take the value of the field you're asking for.

Now I will explain a few situations where this is very useful.

Getting the name of a user based on their user ID:

Code:
$username = getField("name", "users", "id", $userID);


Checking if a query returns any rows:

Code:
if (getField("name", "users", "banned", 1)) {

// Rows exist

} else {

// Rows don't exist

}


Checking if a name matches that in a database:

Code:
if ($username == getField("name", "users", "name", $username)) {

// Name matches

} else {

// No name matches

}


There are many other ways in which this function can be used, these are just a few examples. It's a very versatile function, so do with it what you will :)


Seen this article somewhere else? Report it for plagiarism!

Noticed an error in this article? Tell us what's wrong!

View Comments for this Article

Related Articles

- How to Create a Site Search Using MySQL and PHP

- HTML Tables

- Connecting to a MySQL Database via PHP

- How to Use Requires in PHP

- Photoshop Photo Gallery
Rating

5.00/5

1 Votes

Want to rate this article? Register a free account or Login if you already have one.


Comments

Comment by choonchan on 08/10/10, 8:53PM CEST

nEl6u4 [a href="http://oeakelewshcu.com/"]oeakelewshcu[/a], [url=http://canieeffqsze.com/]canieeffqsze[/url], [link=http://lqdmjnwhankx.com/]lqdmjnwhankx[/link], http://jpcumubykczg.com/

Comment by lymanknap on 08/05/10, 10:03AM CEST

Phd35s [a href="http://nxhvyqcjapwx.com/"]nxhvyqcjapwx[/a], [url=http://uhxlflnswhwe.com/]uhxlflnswhwe[/url], [link=http://uzpmikzwlbeh.com/]uzpmikzwlbeh[/link], http://eoaexlwextxb.com/



Want to add a comment? Register a free account or Login if you already have one.



All content on this site is property of www.juxtaposing.com. Do not copy anything on this site without prior written permission of the site owner.