Q695 Posted September 5, 2008 Share Posted September 5, 2008 Where would I need to put a MySql statement in the following script (combine the two to get one output statement): /////////////////////////////////////////////////////////////////////////////////// $sql="select cid from provider where pid=\"$id\""; $result=@mysql_query($sql,$con) or die(death($sql)); while ($row = @mysql_fetch_array($result)) { $cid = $row["cid"]; echo("<option>" . $cid . "</option>");$cid . "<br>"; } ///////////////////////////////////////////////////////////////////////////////////// $sql="select * from people where id=\"$cid\";"; $result=@mysql_query($sql,$con) or die(death($sql)); while ($row = @mysql_fetch_array($result)) { echo "$sql<P>"; echo("<option>" . $row["first_name"] . " " . $row["last_name"] . "</option>"); } //////////////////////////////////////////////////////////////////////////////////////////// Whenever I change one little thing it seems to create a syntax error for some reason (provider is a xref table) ??? any advice on how to fix my issue, or code to do it . Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/ Share on other sites More sharing options...
Q695 Posted September 6, 2008 Author Share Posted September 6, 2008 The current output is: <select size="1" name="provider" onChange="this.form.provider.value="> <option selected>Select a provider</option><option>1</option><option>3</option></select> Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/#findComment-635003 Share on other sites More sharing options...
Ken2k7 Posted September 6, 2008 Share Posted September 6, 2008 Well $cid doesn't exist in the second query statement if you have it outside the first while loop. Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/#findComment-635404 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Well $cid doesn't exist in the second query statement if you have it outside the first while loop. Well, it DOES, it just contains the last value. You should really try a JOIN. Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/#findComment-635547 Share on other sites More sharing options...
Ken2k7 Posted September 7, 2008 Share Posted September 7, 2008 Well $cid doesn't exist in the second query statement if you have it outside the first while loop. Well, it DOES, it just contains the last value. You should really try a JOIN. Right, sorry. Thanks for the correction. Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/#findComment-635570 Share on other sites More sharing options...
Q695 Posted September 7, 2008 Author Share Posted September 7, 2008 That is what I was trying to do, but how would I write it? Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/#findComment-635622 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 #SQL SELECT * FROM people AS p INNER JOIN provider AS pr ON (pr.cid = p.id) WHERE pr.pid = 1; Change up the WHERE to use your variable. Just put it in where the 1 is. Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/#findComment-636066 Share on other sites More sharing options...
Q695 Posted September 7, 2008 Author Share Posted September 7, 2008 I'm still confused . How would I create the join with my mysql scripts not changing using PHP, since I will need to do something similar several times ??? Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/#findComment-636181 Share on other sites More sharing options...
Q695 Posted September 8, 2008 Author Share Posted September 8, 2008 I figured it out with the help of PHPmyadmin 2.10.1 query editor . now I just need to fix it up a little but here is the basic script: $sql = "SELECT `provider`.`cid`, `people`.`id`, `people`.`first_name`, `people`.`last_name` FROM provider, people WHERE cid = id ORDER BY `people`.`id` ASC;"; $result=@mysql_query($sql,$con) or die(death($sql)); while ($row = @mysql_fetch_array($result)) { $cid = $row["cid"]; $first_name = $row["first_name"]; $last_name = $row["last_name"]; echo ("$first_name $last_name<br>"); Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/#findComment-636305 Share on other sites More sharing options...
DarkWater Posted September 8, 2008 Share Posted September 8, 2008 Mine does the same thing, only it's shorter. Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/#findComment-636451 Share on other sites More sharing options...
magic2goodil Posted September 16, 2008 Share Posted September 16, 2008 You might try: $provider=array(); $people=array(); $sql = "SELECT provider.cid, people.* FROM provider JOIN people ON provider.cid = people.cid WHERE provider.pid = {$id};"; $result=@mysql_query($sql,$con) or die(death($sql)); while($row = @mysql_fetch_assoc($result)) { $provider[] = "<option value=\"{$row['cid']}\">{$row['cid']}</option>"; $people[] = "<option>{$row['first_name']} {$row['last_name']}</option>"; } //Now you can reuse provider and people as you wish. foreach($provider as $value) { echo $value; } foreach($people as $value) { echo $value; } Link to comment https://forums.phpfreaks.com/topic/122877-trouble-combining-two-where-statements-with-mysql-where/#findComment-642488 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.