PHP/MySQL Flash RPG System Help

Hi, I am building a Flash RPG game and using PHP/MySQL for storage/sss. When the player logs onto a character, I want to load their inventory. I have 2 tables, in my database, items and characters. Characters have 20 slots with id numbers of an item in the items table. I then load the item info from the slot. Here is my current code:

(sorry, it’s huge so I had to upload it)
text-upload.com/read,3849516065625

Is there an easier way to load the data? I have tried using loops but have not been able to get it to work…
Thanks!

I doesn’t help your cause to provide so much code you have to provide a link to it. Speaking for myself, I didn’t look and I’m not interested in looking.

To use two tables as you describe you need join in the query. Each time you read in and write out you’ll need to do the lookup dance from one table to another…

Why not build one table for both. Each player’s complete identity and inventory is on one row. So a generic table would have the columns…

id, name, kind, category, rank, health, shield, sword, dagger, rubber_chicken, woopie_cushion, magic_dice and so on…

Then, all you need is a simple query to load or save each players complete package.

Just looking at the start of your code, I could prob shorten it a bit at the start.

Instead of using mysql_result, look at mysql_fetch_array

[php]
include(‘scripts/mysqlconnector.php’);
$result = mysql_query(“SELECT * FROM users WHERE username = '”.$_POST[‘username’]."’") or die();
$Inventory = mysql_fetch_array($result);
[/php]

This would allow to use like $Inventory[‘gold’] to get your value of $vGold.

give me a bit i’ll go through see if i can shorten this more

Just remembered about this. I’m goin to work on it now for you.

Here’s a shortened version. It should do exactly what your one did. You can test it by using a form with username and sending it to this page:

[php]

<?php include('scripts/mysqlconnector.php'); function getAll($result) { if (mysql_num_rows($result) != 0) { while ($row = mysql_fetch_array($result)) { $x[] = $row; } return $x; } else { return null; } } $result = mysql_query("SELECT * FROM users WHERE username = '".mysql_real_escape_string($_POST['username'])."'") or die(); $Inventory = mysql_fetch_array($result); $Query = "SELECT * FROM items "; for ($i=1; $i<=20; $i++) { If ($i == 1) { $Query .= "WHERE item_id = {$Inventory['item'.$i]} "; } Else { $Query .= "OR item_id = {$Inventory['item'.$i]}"; } } $Result = mysql_query($Query) or die(); $Items = getAll($Result); $Final = "gold={$Inventory['gold']}"; for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iName{$x}={$Items[$i]['name']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iIcon{$x}={$Items[$i]['icon']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iLevel{$x}={$Items[$i]['icon']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iDmg{$x}={$Items[$i]['dmg']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iMaxdmg{$x}={$Items[$i]['maxdmg']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iDesc{$x}={$Items[$i]['desc']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iSell{$x}={$Items[$i]['sell']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iUrl{$x}={$Items[$i]['url']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iCost{$x}={$Items[$i]['cost']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iUpgrade{$x}={$Items[$i]['upgrade']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iCoins{$x}={$Items[$i]['coins']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iBonus{$x}={$Items[$i]['bonus']}"; } for ($i=0;$i<20;$i++) { $x=$i+1; $Final .= "&iId{$x}={$Inventory['item'.$i]}"; } $done = true; Echo $Final."&finishedLoadInvent={$done}"; die(); ?>

[/php]

Thank you very much!

Sponsor our Newsletter | Privacy Policy | Terms of Service