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.

Recommended Answers

All 7 Replies

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'

SELECT ip_id FROM test WHERE network = ''

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 you know how to do that?

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

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.

// run the query on the connection
	if (!($result = @ mysql_query ($query, $connection)))

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

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's search box to bring up a page that lists most the MySQL functions.)

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...)

thanks WhiteLeo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.