Jump to content

[SOLVED] Getting data from a Particular Column and Row in a Table


noirsith

Recommended Posts

Right now I can query my table as an array (mysql_fetch_array) and output all the data into a table I create.

 

However if I just wanted to get the data from Column 2, Row 1 of a table in my database and output it not into a table but just as is, is there a way to do that?

 

Thank you much.

Yes, you can do that with mysql_result(). You can either do a blanket

 

$sql = "select * from table";
$result = mysql_query($sql);

$row = 0; // row 1 cuz it starts at 0
$col = 1; // col 2 cuz it starts at 0
$val = mysql_result($result, $row, $col);

 

But that's not really very efficient unless you're wanting to be able to grab multiple pieces of data and the "x,y coords" are variable.

 

If you're looking for retrieving just 1 cell in a table, you should write your query string to return only that cell to begin with.  Example:

 

tNames

id name

0  John

1  Mary

2  Jane

3  Mike

 

$sql = "select name from tNames where id = 1";
$result = mysql_query($sql);
$name = mysql_result($result, 0, 0);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.