chinwan Posted July 28, 2009 Share Posted July 28, 2009 I have two tables: One is 'families', the other is 'buildings'. In the families, there is a field called 'buildings' that relates to entries in the 'buildingid' table. As well as a field called 'phonenumber'. What code would I use to pull up the the phone number of the familes when I am viewing a particular building. I have the first part: "SELECT phonenumber FROM families WHERE buildingid='"$building"; But how do I tell it to store the results. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/167834-solved-how-to-use-select-from-to-store-a-result/ Share on other sites More sharing options...
bruce080 Posted July 28, 2009 Share Posted July 28, 2009 I think you might need to provide more information. If I am reading your post correctly, you want to display all the phone numbers that correspond to a particular building from the 'families' table. Try the following sql statement if that is correct. SELECT phonenumber FROM families WHERE buildings='<building name>'; Quote Link to comment https://forums.phpfreaks.com/topic/167834-solved-how-to-use-select-from-to-store-a-result/#findComment-885204 Share on other sites More sharing options...
chinwan Posted July 28, 2009 Author Share Posted July 28, 2009 The result will only be one phone number. How do I store that result, so I can pull it it up to display it? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/167834-solved-how-to-use-select-from-to-store-a-result/#findComment-885205 Share on other sites More sharing options...
bruce080 Posted July 28, 2009 Share Posted July 28, 2009 Again, you are still being slightly vague, but if there will only be one result, this will work. $result = mysql_query("SELECT phonenumber FROM families WHERE buildingid='"$building";"); while($row = mysql_fetch_array($result)) { $number=$row['phonenumber']; } echo "$number"; Quote Link to comment https://forums.phpfreaks.com/topic/167834-solved-how-to-use-select-from-to-store-a-result/#findComment-885210 Share on other sites More sharing options...
bruce080 Posted July 28, 2009 Share Posted July 28, 2009 There was a slight error with the quotes I used in the query statement of my code. It is fixed below. $result = mysql_query("SELECT phonenumber FROM families WHERE buildingid='$building';"); while($row = mysql_fetch_array($result)) { $number=$row['phonenumber']; } echo "$number"; Quote Link to comment https://forums.phpfreaks.com/topic/167834-solved-how-to-use-select-from-to-store-a-result/#findComment-885217 Share on other sites More sharing options...
chinwan Posted July 28, 2009 Author Share Posted July 28, 2009 I tired that but now I am getting: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in .... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/167834-solved-how-to-use-select-from-to-store-a-result/#findComment-885223 Share on other sites More sharing options...
bruce080 Posted July 28, 2009 Share Posted July 28, 2009 Okay, this error means that your SQL query is failing. Try the following code. The "or die(mysql_error())" will tell us what the problem is. $sql = "SELECT phonenumber FROM families WHERE buildingid='$building';"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $number=$row['phonenumber']; } echo "$number"; Quote Link to comment https://forums.phpfreaks.com/topic/167834-solved-how-to-use-select-from-to-store-a-result/#findComment-885231 Share on other sites More sharing options...
chinwan Posted July 28, 2009 Author Share Posted July 28, 2009 Okay, I got that fixed. How would I have it set up, if I would want it that instead of just pulling up 'phonenumber', it should pull up 'phonenumber' 'contactname' and 'emailaddress'. How would I structure the select command? $sql = "SELECT ???????? FROM families WHERE buildingid='$building';"; Thanks Quote Link to comment https://forums.phpfreaks.com/topic/167834-solved-how-to-use-select-from-to-store-a-result/#findComment-885238 Share on other sites More sharing options...
bruce080 Posted July 28, 2009 Share Posted July 28, 2009 $sql = "SELECT phonenumber, contactname, emailaddress FROM families WHERE buildingid='$building';" Quote Link to comment https://forums.phpfreaks.com/topic/167834-solved-how-to-use-select-from-to-store-a-result/#findComment-885240 Share on other sites More sharing options...
chinwan Posted July 28, 2009 Author Share Posted July 28, 2009 Got it. Thanks a million. Quote Link to comment https://forums.phpfreaks.com/topic/167834-solved-how-to-use-select-from-to-store-a-result/#findComment-885246 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.