garyed Posted March 20, 2010 Share Posted March 20, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/195887-getting-single-piece-of-data-from-msql/ Share on other sites More sharing options...
trq Posted March 20, 2010 Share Posted March 20, 2010 $sql = "select north from wall_type where type='e_Wood Siding R 13';"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $north = mysql_result($result, 0); } } Quote Link to comment https://forums.phpfreaks.com/topic/195887-getting-single-piece-of-data-from-msql/#findComment-1028913 Share on other sites More sharing options...
trq Posted March 20, 2010 Share Posted March 20, 2010 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]; } } Quote Link to comment https://forums.phpfreaks.com/topic/195887-getting-single-piece-of-data-from-msql/#findComment-1028916 Share on other sites More sharing options...
garyed Posted March 20, 2010 Author Share Posted March 20, 2010 Thanks, That works perfectly. Now if I could only understand how that code works. Quote Link to comment https://forums.phpfreaks.com/topic/195887-getting-single-piece-of-data-from-msql/#findComment-1028926 Share on other sites More sharing options...
garyed Posted March 20, 2010 Author Share Posted March 20, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/195887-getting-single-piece-of-data-from-msql/#findComment-1028934 Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 because that can cause errors, if makes sure it runs. Quote Link to comment https://forums.phpfreaks.com/topic/195887-getting-single-piece-of-data-from-msql/#findComment-1028937 Share on other sites More sharing options...
trq Posted March 20, 2010 Share Posted March 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195887-getting-single-piece-of-data-from-msql/#findComment-1028938 Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/195887-getting-single-piece-of-data-from-msql/#findComment-1028941 Share on other sites More sharing options...
garyed Posted March 20, 2010 Author Share Posted March 20, 2010 echo "Now I see "; // says the blind man. Quote Link to comment https://forums.phpfreaks.com/topic/195887-getting-single-piece-of-data-from-msql/#findComment-1028944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.