/    Sign up×
Community /Pin to ProfileBookmark

Can we dynamicaly access a mysql db?

Here is my problem. I want to make a character inventory. Each user that subscribes to my web site has a character. He can buy things, win money, etc. For the money it is very simple, for each recordset a money column is allocated. But what about the inventory? I really don’t know how to do it.

I tried making a table in mysql with all the objects: (the ~ are used to make space between columns in this exemple)
ex:

[B]Item table:[/B]

[B]ID[/B]~~~~~~[B]Name of item[/B]~~~~~~~~ [B]Description[/B]~~~~~~~~~~ [B]Price[/B]
1~~
~~~~~Pencil~~~~~~~~~~~a pencil to write~~~~~~~~~1$
2~~
~~~~~Eraser~~~~~~~~~~an eraser to erase~~~~~~~~0.50$
etc…

and then for each recordset (or each item) make a column in the user table of type ‘int’ so each user has a specified amount of each item he bought
ex:

[B]users table:[/B]

user name~~~~~~~pass~~~~~~ money~~~~~~[I]pencil[/I]~~~~~…~~~~~([I]next column would be the next item in the items table[/I])
lunfel
bobby
megaman21

But for some reason I can’t add the objects dynamicaly into the mysql db.

Here is my PHP code

[code=php]
include(“connect.conf.php”);
$con=mysql_connect($host,$username,$password);
mysql_select_db($bd);

$sql_item=”SELECT nom FROM items”;
$query_item=mysql_query($sql_item,$con); //GET THE ITEMs IN THE ITEMS TABLE

$i=0;
while($i<mysql_num_rows($query_item)) //ADDING THE ITEMS INTO THE DATABASE. LOOPS THE NUMBER OF TIME THERE IS RECORDSETS
{
$result_item=mysql_fetch_array($query_item);
$current_item=$result_item[0]; //PASSING THE VALUE TO A GLOBAL VAR
echo $current_item; //TEMPORARY CODE TO CHECK IF THE VAR HAS A VALUE (AND IT HAS ONE)
@mysql_query(“ALTER TABLE eleves ADD . $current_item . INT(3) DEFAULT ‘0’ NOT NULL”,$con); //ALTERING THE TABLE ELEVES (IN THIS CASE ELEVES IS EQUAL TO MY USERS TABLE) DEPENDING ON ITEM STORED IN $CURRENT_ITEM
$i++; //ITERATION OF MY LOOP
}
[/code]

As you can see, the line where I alter the table there is something wrong

[code=php]@mysql_query(“ALTER TABLE eleves ADD . $current_item . INT(3) DEFAULT ‘0’ NOT NULL”,$con);[/code]

It is not the proper way to add a concocted string as argument. This is one of many possibilities I tried.

I tried all of these:

[code=php]
@mysql_query(“ALTER TABLE eleves ADD” . $current_item . ” INT(3) DEFAULT ‘0’ NOT NULL”,$con);
@mysql_query(“ALTER TABLE eleves ADD . ‘$current_item’ . INT(3) DEFAULT ‘0’ NOT NULL”,$con);
@mysql_query(“ALTER TABLE eleves ADD $current_item INT(3) DEFAULT ‘0’ NOT NULL”,$con);
@mysql_query(“ALTER TABLE eleves ADD ” $current_item ” INT(3) DEFAULT ‘0’ NOT NULL”,$con);
@mysql_query(“ALTER TABLE eleves ADD ‘ “$current_item” ‘ INT(3) DEFAULT ‘0’ NOT NULL”,$con);
[/code]

None of these worked out except one

[code=php]
@mysql_query(“ALTER TABLE eleves ADD . ‘$current_item’ . INT(3) DEFAULT ‘0’ NOT NULL”,$con);
[/code]

Outputing an item with the name $current_item and not his value. All other wouldn’t do anything

When I put the string into a var and then outputed it, it would be the name of the item name that would appeard normally as I wanted but in the db it didn’t make a change
I was using

[code=php]
$myquery=”ALTER TABLE eleves ADD” . $current_item . ” INT(3) DEFAULT ‘0’ NOT NULL”;
echo $myquery;
[/code]

[B][SIZE=3]I really don’t know what to do please help anyone. If you find a way simpler to do what I want to do (make columns using recordsets) please tell me.[/SIZE][/B]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@chazzyAug 21.2006 — you should have a table of just items to user relationships, then a table of types of items - the item to user relationship would be a user owning a particular item.
×

Success!

Help @lunfel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...