Jump to content
MakeWebGames

Variables?


newttster

Recommended Posts

One of the things that I have been struggling to understand is declaring a table as a variable.

By this I mean ... in McCodes the users table is defined as $ir and then you would have the column (or is it field name)['username']. So any time we see $ir['username'] we know that it will pull the users name from the users table.

So far all the things that I have been doing I have been using the existing tables because the table variable is defined. However, I find it extremely cumbersome to keep adding things to tables that already exist ... especially the users table ... that sucker is too full to begin with ... in my opinion.

I've googled declaring variables etc ... but I haven't found anything that specifically answers this.

Could someone explain how this is done properly. Bearing in mind that if you use any words that are bigger than "and" or "the" this noob will get lost. ;)

Thanks.

Link to comment
Share on other sites

Do you mean creating a new $ir? For example like this (I will use the users table, just for an example)

$users['username']? If so, you're looking for a mysql_fetch_array, or a mysql_fetch_object

mysql_fetch_ array is basically the format you're looking for. It gets information from the DB and stored it as an array into the var you provide. So something like this:

$q = mysql_query("SELECT userid, username, money FROM users WHERE userid = {$ir['userid']}");
while($users = mysql_fetch_array($q)) // This basically stores the info from $q into an array called $users. 
{
echo "You are $users['username'] ($users['username']) and you have a total of \$" . number_format($users['money']) . " ";
}

 

Mysql_fetch_object I don't know too much about, but it's used in a different way. Instead of being $users['username'] it would be $users->username

If anyone would care to elaborate more as I think I provide a basic explanation it would be great. Any questions newttster post them here and I'll try to help :)

Link to comment
Share on other sites

No ... not creating a new $ir. But ... if I'm following you correctly, if I had a table called `quests` and a row called `stepone` I could declare it like this;

 

$q = mysql_query("SELECT stepone FROM quests WHERE userid = {$ir['userid']}"); 
while($quests = mysql_fetch_array($q)) 
{
    if ($quest['stepone']=='done')
    {
    echo "You have completed step one.";
    }
    else
    {
    "do something here";
    }
}

 

Would that be the right way to do this?

Link to comment
Share on other sites

No ... not creating a new $ir. But ... if I'm following you correctly, if I had a table called `quests` and a row called `stepone` I could declare it like this;

 

$q = mysql_query("SELECT stepone FROM quests WHERE userid = {$ir['userid']}"); 
while($quests = mysql_fetch_array($q)) 
{
    if ($quest['stepone']=='done')
    {
    echo "You have completed step one.";
    }
    else
    {
    "do something here";
    }
}

 

Would that be the right way to do this?

There's no need for the while loop, as your only pulling one row of data from the table (1 userid).

It then stores it into an array (multiple value variable)..

 

$q = mysql_query("SELECT `stepone` FROM `quests` WHERE `userid` = ".$ir['userid']);
$quests = mysql_fetch_array($q);
if ($quest['stepone']=='done') {
    echo "You have completed step one.";
}
else {
    echo "do something here"; 
}

 

You'd only need the loop if you was pulling more then one row of data.

Edited by Isomerizer
Link to comment
Share on other sites

*nods* I know that as it stands now none of the coding I have done so far is secure.

I am waiting until I have everything working the way I want it to, then I will be attacking the security side of it. It will be a while before that comes about though.

Hmm ... if worse comes to worst ... I'll hire someone from Danny696's trusted list to help me with the security.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...