RE: [PHP-DB] Conditional search from two tables?

From: Date: Wed, 20 Sep 2000 16:06:15 +0000
Subject: RE: [PHP-DB] Conditional search from two tables?
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-2990@lists.php.net to get a copy of this message
> $result8 = mysql_query("SELECT * FROM adresse WHERE > (gate='$gt')",$db); > while ($myrow8 = mysql_fetch_array($result8)) { > echo $myrow8["id"]; > $result9 = mysql_query("SELECT * FROM kommentar WHERE > (id=$myrow8["id"]) ", $db); > $myrow9 = mysql_fetch_array($result9); > echo $myrow9["id"]; > } Use a mysql join...that'll make the code a lot cleaner (untested) $result8 = mysql_query("SELECT t1.id, t2.id FROM adresse AS t1, kommentar AS t2 WHERE t1.gate='$gt' && t1.id = t2.id"); while ($myrow9 = mysql_fetch_row($result8)) { echo "Value 1: $myrow9[0]\nValue 2: $myrow9[1]\n"; } mysql_free_result($result8); This will print all the ids where gate in adresse is equal to $gt (and there must be a matching ID in kommentar). Obviously by adding additional columns in the join from either table...you can expand this to meet your needs. Sincerely, Craig Vincent ------------------------- Senior Programmer Jazzmonkey Media Inc. http://www.jazzmonkey.com ------------------------- > -----Original Message----- > From: Knut Lekvam [mailto:lekvam@sensewave.com] > Sent: Wednesday, September 20, 2000 6:03 AM > To: php-db@lists.php.net > Subject: [PHP-DB] Conditional search from two tables? > > > I have a MySQL table called "adresse" with a tinyint row called > "id". I want to use it in a second search in another table, > "kommentar", wich also has a tinyint row called "id". I used this script: > > $result8 = mysql_query("SELECT * FROM adresse WHERE > (gate='$gt')",$db); > while ($myrow8 = mysql_fetch_array($result8)) { > echo $myrow8["id"]; > $result9 = mysql_query("SELECT * FROM kommentar WHERE > (id=$myrow8["id"]) ", $db); > $myrow9 = mysql_fetch_array($result9); > echo $myrow9["id"]; > } > > This gave the following error: > > "Parse error: parse error, expecting STRING' or > NUM_STRING' or `'$'' in > /home/httpd/html/script/side2.php3 on line 68" > > It works ok if I put in (id=2) instead of (id=$myrow8["id"]) > > (it prints out my first id value in both cases, but it halted on > the second when the error came). > > Why cant I use (id=$myrow8["id"]) here? and how can I pick out > values from the second db based on the first search ? > > I think I have now understood the point in differentiating > between char and int which David Robley and zeles pointed out to > me after my last question. (Now the char variable is working > nice in my first search statement) > > -- > Knut Lekvam > > __________________________________________________________________ > > Denne eposten er sendt ved hjelp av http://www.Organizer.net > - Norges ledende webbaserte kommunikasjonsløsning. > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net > For additional commands, e-mail: php-db-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.db (#2990) next »