954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

retrieving a single cell of data from a MySQL database

Is it possible to retrieve one cell of data from a MySQL database? I need to put the ID from one row of a table into a php variable so that I can recall the correct row later on for editing purposes. If you know how to do this please help me! I will post my code if you ask me to.

sickly_man
Light Poster
42 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

Yes. Just select that one column name and use a WHERE clause to limit it to the one row you want:

SELECT column_name FROM table_name WHERE spme_column = 'some value'

stymiee
He's No Good To Me Dead
Moderator
3,360 posts since May 2006
Reputation Points: 161
Solved Threads: 38
 

[sql]
SELECT ip_id FROM test WHERE network = '10.0.0.1'
[/sql]

so i've got that much already....what i need to do is query the database and get that ip_id number into a php variable or an html hidden input container......do you know how to do that?

sickly_man
Light Poster
42 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

This is pretty basic PHP/MySQL. Have you read any tutorials on this yet? How much code have you written so far?

stymiee
He's No Good To Me Dead
Moderator
3,360 posts since May 2006
Reputation Points: 161
Solved Threads: 38
 

i should be a little more specific. i can get to the point where i have the $result array that has the results of that query we were talking about.

[PHP]
// run the query on the connection
if (!($result = @ mysql_query ($query, $connection)))
showerror();

[/PHP]

i don't know what to do with the $result array to retrieve this one piece of data (an ID). The query only asks for one piece of data so the array should only have one item in it. So that's where I'm at

sickly_man
Light Poster
42 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

Take a look at the mysql_fetch* functions and mysql_result. I've got a feeling that mysql_result will do what you want.

(You can simply type "MySQL" in php.net's search box to bring up a page that lists most the MySQL functions.)

Puckdropper
Posting Pro
500 posts since Jul 2004
Reputation Points: 23
Solved Threads: 23
 

To get a specific thing out of the array, you can use

$array = mysql_fetch_array($result);
$whatever = $array[columnname]

where $result is the executed query, and $columnname is the name of the column you want.

Or if its just one use:

$whatever = mysql_result($result,0);

where 0 is the position of the column you want. (In your SQL statement, like SELECT `name`,`bob`... bob would be 1, name would be 0 ect...)

WhiteLeo
Newbie Poster
16 posts since Feb 2007
Reputation Points: 10
Solved Threads: 2
 

thanks WhiteLeo.

sickly_man
Light Poster
42 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You