Multiple Table Query, while inside a while, results=table length?

I’m trying to display information based on a cross-reference of table data from 3 tables using multiple ‘while’s’. The problem I’m having is that the data inside the innermost ‘while’ is being displayed 6881 times – the exact number of entries in the first table. The rest seems to be working fine.

Working example: http://l2-x.no-ip.org/search.html Enter the term “bone” and hit enter. Then view the details in the “ITEM” column. Repeated identical monster entries.

Tables:

etcitem
item_id (A), name, crystal_type

droplist
mobId (B), itemId (matches up with A), min, max, category, chance

npc
id (matches up with B), name, level

What I’m doing is trying to display items queried b y name or item ID, then for each one, display which monster drops it - by referencing the item’s ID in ‘etcitem’ table to the matching ID in ‘droplist’ table, then take the monster [mobId] and drop chance from that droplist entry, and again reference it to the npc table to display the monster’s name and level)

(In the back of my mind, the 2 ‘names’ in the query might get confused?)

$edrop = mysql_query("SELECT etcitem.item_id, etcitem.name, etcitem.crystal_type, droplist.mobId, droplist.itemId, droplist.min, droplist.max, droplist.category, droplist.chance, npc.id, npc.name, npc.level FROM etcitem, droplist, npc WHERE $eitem = droplist.itemId AND npc.id = droplist.mobId ORDER BY npc.level ASC");

Snippet:

[code]if($method == “EQUALS”)
{
$searche = mysql_query(“SELECT * FROM etcitem WHERE $type = ‘$query’ ORDER BY $omethod $smethod”);
$searcha = mysql_query(“SELECT * FROM armor WHERE $type = ‘$query’ ORDER BY $omethod $smethod”);
$searchw = mysql_query(“SELECT * FROM weapon WHERE $type = ‘$query’ ORDER BY $omethod $smethod”);
} else {
$searche = mysql_query(“SELECT * FROM etcitem WHERE $type LIKE ‘$query’ ORDER BY $omethod $smethod”);
$searcha = mysql_query(“SELECT * FROM armor WHERE $type LIKE ‘$query’ ORDER BY $omethod $smethod”);
$searchw = mysql_query(“SELECT * FROM weapon WHERE $type LIKE ‘$query’ ORDER BY $omethod $smethod”);
}

echo “”;
echo “

”;

$x=1;

if(mysql_num_rows($searche) > 0)
{
echo “

”;
echo “”;
echo “”;
$e=0;

while ($row = mysql_fetch_assoc($searche))
{
echo “<tr style=background-color:green;cursor:move onclick=toggle('y”.$x."’) title=‘Click to toggle info’>

";
$rowup = strtoupper($row[‘crystal_type’]);
echo “”;
$x++;
$e++;
}
echo “
ITEMS
Item ID Name
".$row[‘item_id’]." ".$row[‘name’]."
<div id=y”.$x." style=".$divdef.">Grade: ".$rowup;

$eitem = $row[‘item_id’];

//
$edrop = mysql_query(“SELECT etcitem.item_id, etcitem.name, etcitem.crystal_type, droplist.mobId, droplist.itemId, droplist.min, droplist.max, droplist.category, droplist.chance, npc.id, npc.name, npc.level FROM etcitem,droplist,npc WHERE $eitem = droplist.itemId AND npc.id = droplist.mobId ORDER BY npc.level ASC”);
if(mysql_num_rows($edrop) > 0)
{
while ($edrow = mysql_fetch_assoc($edrop))
{
echo “
Monster: “.$edrow[‘name’].” Lvl: “.$edrow[‘level’];
if($edrow[‘category’] == -1)
{
$edtype = “Spoiled”;
} else {
$edtype = “Dropped”;
}
$edper = ($edrow[‘chance’] / 10000);
echo “
”.$edtype.” “.$edper.”%”;
}
}
//

echo “

”;
} else {
echo “
No results found!
”;
}[/code]

[hr]

ENTIRE CODE::

[code]

body { scrollbar-base-color: #888888; scrollbar-arrow-color: #888888; scrollbar-3dlight-color: #888888; scrollbar-darkshadow-color: #888888; scrollbar-face-color: #000000; scrollbar-highlight-color: #000000; scrollbar-shadow-color: #888888; scrollbar-track-color: #888888; } <?php include 'ip.php'; $connect = @mysql_pconnect( $server, $username, $password ) or die(); $db_select = @mysql_select_db( $database, $connect ) or die(); //show details by default? (0 or 1) $show = 1; if($show == 1) { $divdef = "display:block"; } else { $divdef = "display:none"; } $a=0; $e=0; $w=0; $query = mysql_real_escape_string($_POST['query']); $type = mysql_real_escape_string($_POST['type']); $method = mysql_real_escape_string($_POST['method']); $smethod = mysql_real_escape_string($_POST['smethod']); $omethod = mysql_real_escape_string($_POST['omethod']); if($method == "CONT") { $queryx = "%"; $query .= $queryx; $queryx .= $query; $query = $queryx; $queryx = ""; } if($method == "BEGIN") { $queryx = "%"; $query .= $queryx; $queryx = ""; } if($method == "END") { $queryx = "%"; $queryx .= $query; $query = $queryx; $queryx = ""; } if($method == "EQUALS") { $searche = mysql_query("SELECT * FROM etcitem WHERE $type = '$query' ORDER BY $omethod $smethod"); $searcha = mysql_query("SELECT * FROM armor WHERE $type = '$query' ORDER BY $omethod $smethod"); $searchw = mysql_query("SELECT * FROM weapon WHERE $type = '$query' ORDER BY $omethod $smethod"); } else { $searche = mysql_query("SELECT * FROM etcitem WHERE $type LIKE '$query' ORDER BY $omethod $smethod"); $searcha = mysql_query("SELECT * FROM armor WHERE $type LIKE '$query' ORDER BY $omethod $smethod"); $searchw = mysql_query("SELECT * FROM weapon WHERE $type LIKE '$query' ORDER BY $omethod $smethod"); } echo ""; echo "
"; $x=1; if(mysql_num_rows($searche) > 0) { echo ""; echo ""; echo ""; $e=0; while ($row = mysql_fetch_assoc($searche)) { echo ""; $rowup = strtoupper($row['crystal_type']); echo ""; $x++; $e++; } echo "
ITEMS
Item ID Name
".$row['item_id']." ".$row['name']."
Grade: ".$rowup; $eitem = $row['item_id']; // $edrop = mysql_query("SELECT etcitem.item_id, etcitem.name, etcitem.crystal_type, droplist.mobId, droplist.itemId, droplist.min, droplist.max, droplist.category, droplist.chance, npc.id, npc.name, npc.level FROM etcitem,droplist,npc WHERE $eitem = droplist.itemId AND npc.id = droplist.mobId ORDER BY npc.level ASC"); if(mysql_num_rows($edrop) > 0) { while ($edrow = mysql_fetch_assoc($edrop)) { echo "
Monster: ".$edrow['name']." Lvl: ".$edrow['level']; if($edrow['category'] == -1) { $edtype = "Spoiled"; } else { $edtype = "Dropped"; } $edper = ($edrow['chance'] / 10000); echo "
".$edtype." ".$edper."%"; } } // echo "
"; } else { echo "
No results found!
"; } echo "
"; if(mysql_num_rows($searcha) > 0) { echo ""; echo ""; echo ""; $a=0; while ($row = mysql_fetch_assoc($searcha)) { echo ""; $rowup = strtoupper($row['crystal_type']); $rowbody = strtoupper($row['bodypart']); if($rowbody == "REAR,LEAR") { $rowbody = "EARRING"; } if($rowbody == "RFINGER,LFINGER") { $rowbody = "RING"; } if($rowbody == "NECK") { $rowbody = "NECKLACE"; } if($rowbody == "FEET") { $rowbody = "BOOTS"; } if($rowbody == "DHAIR") { $rowbody = "MASK/HAIR"; } if($rowbody == "HAIR") { $rowbody = "MASK/HAIR"; } if($rowbody == "FACE") { $rowbody = "MASK/HAIR"; } $rowtype = strtoupper($row['armor_type']); echo ""; $x++; $a++; } echo "
ARMORS
Item ID Name
".$row['item_id']." ".$row['name']."
Grade: ".$rowup." Body: ".$rowbody."
Type: ".$rowtype." P.Def: ".$row['p_def']." M.Def: ".$row['m_def']."
"; } else { echo "
No results found!
"; } echo "
"; if(mysql_num_rows($searchw) > 0) { echo ""; echo ""; echo ""; $w=0; while ($row = mysql_fetch_assoc($searchw)) { if($row['bodypart'] == "rhand") { $wield = "1-Hand"; } if($row['bodypart'] == "lrhand") { $wield = "2-Handed"; } if($row['bodypart'] == "lhand") { $wield = "Shield"; } echo ""; $rowup = strtoupper($row['crystal_type']); echo ""; $x++; $w++; } echo "
WEAPONS
Item ID Name
".$row['item_id']." ".$row['name']."
Grade: ".$rowup." Wield: ".@$wield; if($wield == "Shield") { echo "
Shield Defense: ".$row['shield_def']." Shield Defense Rate: ".$row['shield_def_rate']; } else { echo "
P.Dmg: ".$row['p_dam']." M.Dmg: ".$row['m_dam']; } echo "
"; } else { echo "
No results found!
"; } echo "
"; $t = (($e) + ($a) + ($w)); if($t == 0) { $t = "no"; } echo ""; mysql_close(); ?> [/code]

First things first. You are using deprecated mysql_* calls. Use PDO. You can get a bumpstart on on PDO code here:

http://www.phphelp.com/forum/the-occasional-tutorial/beginners-pdo-bumpstart-code-use-it-now!

We cannot support deprecated code.

Second. You only need one query for each condition, not three. You need to use “AND”

I suspect your database design is not correct. Post an sql dump of your tables.

Step one, post your table sql. We need to get that correct so we have a solid foundation to build on.
Step two, copy the PDO files to your system, set the database connection parameters and we will go from there. I will post back an optimized database for you.

I’m entirely confused with that PDO stuff. :confused:

Can you point out exactly where I’d use these AND’s? My issue is entirely contained within the Snippet section in my post. When you mentioned conditions, are you referring to the 3 tables in the $edrop query?


sqldump.zip (466 KB)

I am putting together a download of a complete working PDO setup so you don’t have to copy individual posts. I should have it ready within the hour. PDO will be easy with the download I will provide and me guiding you through it. It will be well worth you taking a little time to get familiar with it.

I am more than happy to take the time to get you where you need to go, but neither I, nor anyone else should help getting bad code working.

So that I can help with what you are specifically doing, please post your table sql. I will have the download available shortly.

The download is attached to my previous post, labelled “sqldump.zip” in the attachments.

Sorry, didn’t see it before. I am going to include bootstrap with my demo. If you don’t know what that is, it will be a nice surprise and something else really cool to learn, but you wont have to. It will just work.

OKAY! The Bootstrapped PDO Bump Start Version 1.0 is ready for download! Sample database included. You will be up and running in seconds.

While I look over your database structure run the demo and look over the code. Best to use an editor with syntax highlighting. A nice easy to use one is EditPlus http://www.editplus.com/


pdo_bumpstart_ver1.0.zip (211 KB)

Looking at your database.

First problem:

Your first form field for type can be for name or item_id. Your first 6 query’s are asking three tables for data based on one of the two column names (Code lines 86-92). The problem is that all three tables don’t have those column names or item_id is spelled different. You are mixing camelCase naming and under_score naming. Pick ONE style and stick to it forever. I prefer under_score as it is easier to read in my opinion.

You judge for yourself:

[code]I_prefer_under_score_as_it_is_easier_to_read_in_my_opinion

iPreferUnderScoreAsItIsEasierToReadInMyOpinion[/code]

  1. Straighten out all those references in your database and your code, especially the item_id, itemID. Check all the other form fields as well.
  2. Repost your modified code and modified database dump so I can stay in sync with you.
  3. We will get to the PDO after we fix the naming bugs
  • Tip: Pick a naming convention and stick to it

Second Problem

You have numerous duplicate values in your database. We will need to do what is called “Database Normalization”. We will get to that later.

Wiki:
Database normalization is the process of organizing the fields and tables of a relational database to minimize redundancy and dependency. Normalization usually involves dividing large tables into smaller (and less redundant) tables and defining relationships between them. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database using the defined relationships.

Sponsor our Newsletter | Privacy Policy | Terms of Service