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
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];
  }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.