Jump to content

getting single piece of data from msql?


garyed

Recommended Posts

I assume this is simple but I can't find a way to do it yet.

All I want to do is get one piece of data out of a mysql database & put it into a variable.

I know how to use mysql_fetch_array but I don't know how to get what I need out of the array.

If I'm on the mysql command line I could type:

mysql> select * from wall_type;
+-------------------------+------+-------+-------+-----------+-------+-------+
| type               | name | north | south | east_west | ne_nw | se_sw |
+-------------------------+------+-------+-------+-----------+-------+-------+
| a_Stucco_R11      | a_St |  0.10 |  0.15 |      0.20 |  0.25 |  0.30 | 
| b_Stucco R13      | b_St |  0.13 |  0.18 |      0.23 |  0.28 |  0.33 | 
| c_Stucco R19      | c_St |  0.19 |  0.24 |      0.29 |  0.34 |  0.39 | 
| d_Wood Siding R11 | d_W  |  1.10 |  0.60 |      2.10 |  2.60 |  3.10 | 
| e_Wood Siding R 13| e_W  |  1.30 |  1.80 |      2.30 |  2.80 |  3.30 | 
| f_Wood Siding R19 | f_W  |  1.90 |  2.40 |      2.90 |  3.40 |  3.90 | 
+--------------------------+------+-------+-------+-----------+-------+-------+
6 rows in set (0.00 sec)

mysql> select north from wall_type where type='e_Wood Siding R 13';
+-------+
| north |
+-------+
|  1.30 | 
+-------+

to get the figure "1.30"

What would be the php code to get that figure "1.30" into a variable?

Link to comment
https://forums.phpfreaks.com/topic/195887-getting-single-piece-of-data-from-msql/
Share on other sites

Oh, and if you wanted to use mysql_fetch_assoc.

 

$sql = "select north from wall_type where type='e_Wood Siding R 13';";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_assoc($result);
    $north = $row['north'];
  }
}

 

Or mysql_fetch_array would be either....

 

[code=php:0]
$sql = "select north from wall_type where type='e_Wood Siding R 13';";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_array($result);
    $north = $row['north'];
  }
}

 

or

 

[code=php:0]
$sql = "select north from wall_type where type='e_Wood Siding R 13';";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_array($result);
    $north = $row[0];
  }
}

Why do I need the if statements?

What's wrong with doing this?

$sql = "select north from wall_type where type='e_Wood Siding R 13';";
$result = mysql_query($sql);
$north = mysql_result($result, 0);

 

 

You should always check your queries succeed and that they actually return results before trying to use them.

Why do I need the if statements?

What's wrong with doing this?

$sql = "select north from wall_type where type='e_Wood Siding R 13';";
$result = mysql_query($sql);
$north = mysql_result($result, 0);

 

 

You should always check your queries succeed and that they actually return results before trying to use them.

 

or you will cause yourself more hassle in cleaning up the errors. harder to find them fix one get another one

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.