Avian Gamers Network

Forum
It is currently Wed May 07, 2025 1:39 pm

All times are UTC - 5 hours [ DST ]




Post new topic Reply to topic  [ 17 posts ] 
Author Message
 Post subject: PHP Help
PostPosted: Tue Aug 24, 2004 4:18 pm 
Offline
Site Admin
User avatar

Joined: Mon Jul 01, 2002 4:33 am
Posts: 6698
Location: Silver Spring, MD
I have two tables I need to search through and see if an id exists in one but not the other.
npc.npcid and npc_2_loot.npcid

I need to be sure that all npc.npcid appears at least once in npc_2_loot.npcid.
If It does not appear once I need it to list out the ones that did not show up in a search.

_________________
Moge


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 4:28 pm 
Offline
Site Admin
User avatar

Joined: Mon Jul 01, 2002 4:33 am
Posts: 6698
Location: Silver Spring, MD
/blows dust off the not often used LEFT JOIN SQL command :?

Code:
$query_rs_npc = "SELECT npc.npcid
FROM npc
LEFT JOIN
npc_2_loot
ON npc_2_loot.npcid = npc.npcid
WHERE npc_2_loot.npcid IS NULL
ORDER BY npc.npcid ASC";

_________________
Moge


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 4:30 pm 
Offline

Joined: Mon Jul 22, 2002 1:10 pm
Posts: 543
Location: Atlanta, GA
SELECT npc.npcid FROM npc WHERE npc.npcid NOT IN ( SELECT DISTINCT npc_2_loot.npcid FROM npc_2_loot );

_________________
The SWG Discussionwill no longer be of any concern to us. I've just received word that the Emperor has dissolved the board permanently. The last remnants of the Old Boards have been swept away.

The regional devs now have direct control over threads. Inability to post will keep the local systems in line.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 4:32 pm 
Offline
Site Admin
User avatar

Joined: Wed Aug 08, 2001 8:01 pm
Posts: 5315
Location: Dublin
Select all the ids from npc and then do a do while loop. Inside the loop make a second database query where you search for the specific id in npc_2_loot (the loop will go through each id returned from the first query). If you don't return a result print a message.

or a left join :P


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 4:33 pm 
Offline

Joined: Mon Jul 22, 2002 1:10 pm
Posts: 543
Location: Atlanta, GA
iJasonT wrote:
/blows dust off the not often used LEFT JOIN SQL command :?

Code:
$query_rs_npc = "SELECT npc.npcid
FROM npc
LEFT JOIN
npc_2_loot
ON npc_2_loot.npcid = npc.npcid
WHERE npc_2_loot.npcid IS NULL
ORDER BY npc.npcid ASC";


I dont think that would ever actually give you results. That would assume that npc.npcid is null on the npc table too. If you drop the WHERE clause completely, though, and do:

SELECT npc.npcid, npc_2_loot.npcid AS loot_id
FROM npc
LEFT JOIN
npc_2_loot
ON npc_2_loot.npcid = npc.npcid
ORDER BY npc.npcid ASC

You could cycle through the results and look for npcid == somethign and loot_id == null.

_________________
The SWG Discussionwill no longer be of any concern to us. I've just received word that the Emperor has dissolved the board permanently. The last remnants of the Old Boards have been swept away.

The regional devs now have direct control over threads. Inability to post will keep the local systems in line.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 4:35 pm 
Offline
Site Admin
User avatar

Joined: Mon Jul 01, 2002 4:33 am
Posts: 6698
Location: Silver Spring, MD
hehe three posts three answers that all work. Man you gotta love PHP.

OK here is an easy one. I don't use Functions much so Im a bit rusty here.
I get 6 results but only 5 are listed via my loop. I had this problem before where the first result is skipped, what do I do to get all 6 to list out?

_________________
Moge


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 4:35 pm 
Offline
Spammer
User avatar

Joined: Sat Nov 30, 2002 3:45 am
Posts: 6302
Location: Raleigh, NC
kebernet wrote:
SELECT npc.npcid FROM npc WHERE npc.npcid NOT IN ( SELECT DISTINCT npc_2_loot.npcid FROM npc_2_loot );

SELECT npc.npcid FROM npc WHERE NOT EXISTS( SELECT npc_2_loot.npcid FROM npc_2_loot WHERE npc.npcid = npc_2_loot.npcid);[/

_________________
Dalaran: Arindel - Frost Mage (Mining/Alchemy)
Dalaran: Roran - Paladin (Weaponsmith/Blacksmith)
Dalaran: Baine - Rogue


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 4:44 pm 
Offline
Site Admin
User avatar

Joined: Mon Jul 01, 2002 4:33 am
Posts: 6698
Location: Silver Spring, MD
Keb. It worked for me fine. I'm loading in a the other examples here to see if I get differing results.

_________________
Moge


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 4:44 pm 
Offline

Joined: Mon Jul 22, 2002 1:10 pm
Posts: 543
Location: Atlanta, GA
iJasonT wrote:
hehe three posts three answers that all work. Man you gotta love PHP.

OK here is an easy one. I don't use Functions much so Im a bit rusty here.
I get 6 results but only 5 are listed via my loop. I had this problem before where the first result is skipped, what do I do to get all 6 to list out?


Im not sure how PHP does SQL resultsets. I think you just get an indexed array of the results. The big thing is, you have to do your null check as the loop control. Something like:

while( $row = fetch_array( $resultset ) ){

// handle loop.

}

If you are wanting to call a function I *think* you can just do a null check on the results set:

if( $resultset ){
my_function( $resultset );
}

_________________
The SWG Discussionwill no longer be of any concern to us. I've just received word that the Emperor has dissolved the board permanently. The last remnants of the Old Boards have been swept away.

The regional devs now have direct control over threads. Inability to post will keep the local systems in line.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 4:46 pm 
Offline

Joined: Mon Jul 22, 2002 1:10 pm
Posts: 543
Location: Atlanta, GA
Arindel wrote:
kebernet wrote:
SELECT npc.npcid FROM npc WHERE npc.npcid NOT IN ( SELECT DISTINCT npc_2_loot.npcid FROM npc_2_loot );

SELECT npc.npcid FROM npc WHERE NOT EXISTS( SELECT npc_2_loot.npcid FROM npc_2_loot WHERE npc.npcid = npc_2_loot.npcid);[/


That would work too, but if I were at the office :P I would say adding a comparison in a nested select where you dont need one adds unneeded DB overhead :P

_________________
The SWG Discussionwill no longer be of any concern to us. I've just received word that the Emperor has dissolved the board permanently. The last remnants of the Old Boards have been swept away.

The regional devs now have direct control over threads. Inability to post will keep the local systems in line.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 5:02 pm 
Offline
Site Admin
User avatar

Joined: Mon Jul 01, 2002 4:33 am
Posts: 6698
Location: Silver Spring, MD
I am having trouble with this one and cant seem to find my syntax error.

Code:
$query_rs_npc = "SELECT npc.npcid
FROM npc
WHERE npc.npcid NOT IN (SELECT DISTINCT npc_2_loot.npcid FROM npc_2_loot)";
$row_npc = mysql_query($query_rs_npc) or die(mysql_error());
$row_rs_npc = mysql_fetch_assoc($row_npc);
$rs_npc = mysql_num_rows($row_npc);
echo $rs_npc;




Code:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN (SELECT DISTINCT npc_2_loot.npcid FROM npc_2_loot)' at line

_________________
Moge


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 5:04 pm 
Offline

Joined: Mon Jul 22, 2002 1:10 pm
Posts: 543
Location: Atlanta, GA
Aaah. Yeah. Most version of MySQL don't support nested selects :P Im too used to Oracle :P

_________________
The SWG Discussionwill no longer be of any concern to us. I've just received word that the Emperor has dissolved the board permanently. The last remnants of the Old Boards have been swept away.

The regional devs now have direct control over threads. Inability to post will keep the local systems in line.


Last edited by kebernet on Tue Aug 24, 2004 5:12 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 5:04 pm 
Offline
Spammer
User avatar

Joined: Sat Nov 30, 2002 3:45 am
Posts: 6302
Location: Raleigh, NC
That depends if the DB in question does a full select statement run for each row you are checking. :P

The Exists function usually forces it to a signle point in the where clause. But, I usually like your approach as it reads a bit easier. Just offering other possibilities :)

and I don't see any Syntax error unless it is looking for a end/terminator for the SQL where (like a semicolon). Even then I don't think it would be around the IN statement

_________________
Dalaran: Arindel - Frost Mage (Mining/Alchemy)
Dalaran: Roran - Paladin (Weaponsmith/Blacksmith)
Dalaran: Baine - Rogue


Last edited by Arindel on Tue Aug 24, 2004 5:08 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 5:06 pm 
Offline
Site Admin
User avatar

Joined: Mon Jul 01, 2002 4:33 am
Posts: 6698
Location: Silver Spring, MD
yeah I have the WHERE npc.npcid NOT IN (was an error in the above post) but it's still no joy.

_________________
Moge


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 5:15 pm 
Offline
Site Admin
User avatar

Joined: Mon Jul 01, 2002 4:33 am
Posts: 6698
Location: Silver Spring, MD
ahh nesting is the problem. Obbo's and mine seem to return the same results so I think all is good. Still having a wierd issue where the first result is not being listed. Very odd.

_________________
Moge


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 2:44 am 
Offline
Site Admin
User avatar

Joined: Mon Jul 01, 2002 4:33 am
Posts: 6698
Location: Silver Spring, MD
ok I need more help.
I am trying to organize my data into a table. I have 2 db tables I am querying. groupnames and grouploot
basically I need it to have 4 columns groupnames over grouploot. There will probably be multiple rows as I have 10-12 grouptitles so far and will probably be adding more.

If you know of a tutorial that can walk me through setting this I would appreciate it.

_________________
Moge


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 2:54 pm 
Offline
Site Admin
User avatar

Joined: Mon Jul 01, 2002 4:33 am
Posts: 6698
Location: Silver Spring, MD
I had it close when I posted here first I just couldn't get my array right. Im pretty new to Arrays so it took me a while but I came up with this. Works great.

Code:
$sql = "SELECT s.grouptitle, l.groupname
      FROM sgroup s INNER JOIN loot_drop l
      ON s.sgid = l.sgid";
$res = mysql_query($sql) or die(mysql_error());
#
# store names in array for each title
# it makes the table columns code a lot simpler
#
$array = array();
while (list($t, $n) = mysql_fetch_row($res)) {
      $array[$t][] = $n;
}

#
# now output the array into a 4 column table
#

define ("NUMCOLS",4);

$count = 0;

echo "<table border='0' cellpadding='4' cellspacing='2' width='500'>\n";
foreach ($array as $title => $names) {
    # new row every NUMCOLS cells
    if ($count % NUMCOLS == 0) echo "<TR valign='top'>\n";
    echo "  <TD width='25%'>\n";
    echo "  <div style='border: solid 1pt silver'>$title</div>\n";

    # now names for the title
    foreach ($names as $n) {
         echo "    $n<br/>\n";
    }

    echo "    <br/>\n  </TD>\n";
    $count++;

    # end row if we've just done NUMCOLs cells
    if ($count % NUMCOLS == 0) echo "</TR>\n";
}

if ($count % NUMCOLS != 0) {
   # end row if not already ended
   while ($count++ % NUMCOLS) echo "<td>&nbsp;</td>";
   echo "</TR>\n";
}

echo "</table>\n";

_________________
Moge


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ] 

All times are UTC - 5 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group