Jump to content

[SOLVED] How to use "select from" to store a result


chinwan

Recommended Posts

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

 

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>';

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";

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";

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";

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

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.