Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,487 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,700 people online right now. Registration is fast and FREE... Join Now!




2 calls to db, 2nd relies on first. i'm an idiot.

 
Reply to this topicStart new topic

2 calls to db, 2nd relies on first. i'm an idiot.

capty99
24 Feb, 2008 - 04:51 PM
Post #1

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,164



Thanked: 16 times
Dream Kudos: 550
My Contributions
its been forever and a day since i used php ... so be kind and rewind.

i have 2 queries. the second relying on the results of the first. i want to pull data in the second depending on what the 'cname' is of the first query.
how do i go about doing this.... i am being really stupid.

php

//first query
$query = "SELECT id, cname, csubhead, caddress, chours, ccontact, coverview, curate, cunumrate, cyamrate, cweb, cglat, cglong, cstores, cgzoom FROM shopcenter WHERE id=2";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());


// if records present
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print article titles
while($row = mysql_fetch_object($result))
{
//second query
$query2 = "SELECT id, sname, stype1, scenter1 FROM stores WHERE scenter1 = $row-> cname ORDER BY sname";
$result2 = mysql_query($query2) or die ("Error in query: $query. " . mysql_error());

User is offlineProfile CardPM
+Quote Post

baavgai
RE: 2 Calls To Db, 2nd Relies On First. I'm An Idiot.
24 Feb, 2008 - 05:08 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Assuming scenter1 is a char type, you probably want something like:
php

$query2 = "SELECT id, sname, stype1, scenter1 FROM stores WHERE scenter1 = '" . $row->cname . "' ORDER BY sname";


However, you should reasonably be able to do with one query:
php

$query = "SELECT a.id, a.sname, a.stype1, a.scenter1"
. " FROM stores a"
. " INNER JOIN shopcenter b ON b.cname=a.scenter1 and b.id=2"
. " ORDER BY a.sname"
;


Hope this helps.

This post has been edited by baavgai: 24 Feb, 2008 - 05:08 PM
User is offlineProfile CardPM
+Quote Post

capty99
RE: 2 Calls To Db, 2nd Relies On First. I'm An Idiot.
24 Feb, 2008 - 05:37 PM
Post #3

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,164



Thanked: 16 times
Dream Kudos: 550
My Contributions
thanks, got that to run.
might have a question about the second part later, but i got it working now.

hoo ya.
User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: 2 Calls To Db, 2nd Relies On First. I'm An Idiot.
24 Feb, 2008 - 05:37 PM
Post #4

D.I.C Regular
Group Icon

Joined: 20 Feb, 2003
Posts: 270

imagine the space in the second query was the issue, 2 fixes posted will fix it.
$row-> cname
User is offlineProfile CardPM
+Quote Post

capty99
RE: 2 Calls To Db, 2nd Relies On First. I'm An Idiot.
24 Feb, 2008 - 05:45 PM
Post #5

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,164



Thanked: 16 times
Dream Kudos: 550
My Contributions
?
User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: 2 Calls To Db, 2nd Relies On First. I'm An Idiot.
24 Feb, 2008 - 06:01 PM
Post #6

D.I.C Regular
Group Icon

Joined: 20 Feb, 2003
Posts: 270

some words in front if that way be of use. lol.

brackets fix many issues in eval strings.
but eval strings are slower and easeir to compromise.

QUOTE(capty99 @ 24 Feb, 2008 - 06:45 PM) *

?


This post has been edited by SpaceMan: 24 Feb, 2008 - 06:03 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:31PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month