Sydcomebak Posted July 3, 2008 Share Posted July 3, 2008 You arrived at this page from a link where you selected "B" meaning that you want to list all residents whose last name starts with "B" Assume the following structure: PeopleTbl -NameID (unique) -NameLast -NameFirst ResidentTbl -ResidentID (unique) -PeopleID -HouseID HousesTbl -HouseID (unique) -Address The following variables are brought in from a link: -$upbound = B -$lowbound = C All residents are people, but not all people are residents. I want to list all of the last names, first names of the people whose PeopleID is found in the ResidentTbl under PeopleID (and their name is between $upbound and $lowbound variables). The HouseIDs for all residents that are listed will be passed along through the names being clickable. All help is appreciated! -Dave Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/ Share on other sites More sharing options...
ratcateme Posted July 3, 2008 Share Posted July 3, 2008 you could get a list of ID numbers from the ResidentTbl then put each one into this query $query = "SELECT * FROM `PeopleTbl` WHERE `NameLast` = '$upbound%' AND `ID` = '{$id}';"; there is also properly a way to get mysql to do the second query when it is selecting the ID's from ResidentTbl but i don't know how Scott. Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/#findComment-581143 Share on other sites More sharing options...
Sydcomebak Posted July 3, 2008 Author Share Posted July 3, 2008 OK, maybe I should have said "Super-Newbie" in the title... =) I hadn't thought of going to the PeopleTbl first, but that certainly makes sense.. the query I have now is: <?php // Request the text of all the names $result = mysql_query("SELECT NameLast, NameFirst FROM PeopleTbl"); if (!$result) { echo("<P>Error performing query: " . mysql_error() . "</P>"); exit(); } // Display the text of each name in a paragraph while ( $row = mysql_fetch_array($result) ) { // get only the names we want if ($row["NameLast"]<$upbound) { if ($row["NameLast"]>$lowbound) { echo("<P>" . $row["NameLast"] . ", " . $row["NameFirst"] . "</P>"); } } } ?> But this was a test query where I was finding <i>people</i> and not <i>residents</i>. I now want to limit the people displayed to the ones whose ID is also present in the ResidentsTbl. Hmm, the gears are turning... Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/#findComment-581157 Share on other sites More sharing options...
libertyct Posted July 3, 2008 Share Posted July 3, 2008 SELECT NameLast, NameFirst FROM tblPeople INNER JOIN tblResident ON (tblPeople.PeopleID = tblResident.PeopleID) WHERE SUBSTRING(NameLast,1,1) = $variableB key is the INNER JOIN Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/#findComment-581162 Share on other sites More sharing options...
Sydcomebak Posted July 3, 2008 Author Share Posted July 3, 2008 SELECT NameLast, NameFirst FROM tblPeople INNER JOIN tblResident ON (tblPeople.PeopleID = tblResident.PeopleID) WHERE SUBSTRING(NameLast,1,1) = $variableB key is the INNER JOIN That looks /too/ simple. *smacks forehead* OK, let me try and intertwine that with the upper and lower bounds and see what happens. Thanks so much! Edit: Ah, looks like you even took care of the upper and lower bound for me by checking the string within the NameLast... Well played... Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/#findComment-581166 Share on other sites More sharing options...
Sydcomebak Posted July 3, 2008 Author Share Posted July 3, 2008 I renamed all the variables to match my tables. I also went ahead and changed the page that links to this one to pass only 1 variable. <?php SELECT NameLast, NameFirst FROM PeopleTbl INNER JOIN ResidentTbl ON (PeopleTbl.PeopleID = ResidentTbl.PeopleID) //50 WHERE SUBSTRING(NameLast,1,1) = $var echo("<P>" . $row["NameLast"] . ", " . $row["NameFirst"] . "</P>"); ?> Why do I feel like I am missing something like: $result = mysql_query( and a while ( $row = mysql_fetch_array($result) ) { ? I'm trying to pick up this stuff as I go along. Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/#findComment-581199 Share on other sites More sharing options...
libertyct Posted July 3, 2008 Share Posted July 3, 2008 oh lol ok $result = mysql_query("SELECT NameLast, NameFirst FROM PeopleTbl INNER JOIN ResidentTbl ON (PeopleTbl.PeopleID = ResidentTbl.PeopleID) WHERE SUBSTRING(NameLast,1,1) = $var"); while ( $row = mysql_fetch_array($result) ) { echo $row['NameLast']." ".$row['NameFirst']; echo "<br>"; } this prints something like: Smith John Adams Tom Chisa Joan etc.... Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/#findComment-581227 Share on other sites More sharing options...
Sydcomebak Posted July 3, 2008 Author Share Posted July 3, 2008 <b>mysql_fetch_array(): supplied argument is not a valid MySQL result resource in ...</b> Hmm, I'm double and tripple checking the variable names and nothing seems wrong. Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/#findComment-581237 Share on other sites More sharing options...
discomatt Posted July 3, 2008 Share Posted July 3, 2008 This means your mysql_query returned FALSE. Use mysql_error() to find out what went wrong in your query. Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/#findComment-581241 Share on other sites More sharing options...
Sydcomebak Posted July 3, 2008 Author Share Posted July 3, 2008 I renamed a couple things and thought that this would fix things, but I'm getting the same error. <?php $result = mysql_query("SELECT NameLast, NameFirst, NameID FROM PeopleTbl INNER JOIN ResidentTbl ON (PeopleTbl.NameID = ResidentTbl.NameID) WHERE SUBSTRING(NameLast,1,1) = $var"); while ($row = mysql_fetch_array($result) ) { echo $row['NameLast']." ".$row['NameFirst']; echo "<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/#findComment-581287 Share on other sites More sharing options...
Sydcomebak Posted July 4, 2008 Author Share Posted July 4, 2008 OK, When I ran the script through MySQL, It told me that the columns were ambiguous. I changed the name of a column, and it went through cleanly. My data showed up just fine. <?php $result = mysql_query("SELECT * FROM PeopleTbl INNER JOIN ResidentTbl ON (PeopleTbl.NameID = ResidentTbl.Name_ID) WHERE SUBSTRING(NameLast,1,1) = $var"); ?> Unfortunately, as soon as I go back to the HTML, it still gives me an error: <b>mysql_fetch_array(): supplied argument is not a valid MySQL result resource in </b>...<b>on line 27</b> Line 27 starts the fetch that follows: <?php while ($row = mysql_fetch_array($result) ) { echo $row['NameLast']." ".$row['NameFirst']; echo "<br>"; } ?> Am I missing something? Link to comment https://forums.phpfreaks.com/topic/113125-solved-php-newbie-trying-to-limit-selections/#findComment-581408 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.