Jump to content
MakeWebGames

How to....


Joel
 Share

Recommended Posts

OK, This may be a noob question but I'm having trouble, Its the only trouble I'm having,

I'm creating my own game engine and what am having trouble is getting stuff from Mysql database,

Say that I want my username and the other users usernames to show on a page,

ect:

Name: Extermination [1]

what would I have to do, I've tried many things, I have got a Login page, Register Page, and a Main Menu page, I want it to show on the Main Menu page, the login and register work fine, I just need help doing this,

I want it to get the Username from the SQL Database on to my main menu page, Hope you understand what I mean, I dont know everything to do with mysql and php, So any help would be nice,

Thanks

Link to comment
Share on other sites

Re: How to....

Thanks Zeon but I have already read threw those sites,

I'm not really hopeing to get anywhere fast, I like to go slow, But while I create the engine I will learn new things, This is probaly the only thing im going to have trouble with, Creating mods is simple and other features ect, But I will go over my engine to secure it (when I have finally know how to secure).

Thanks anyway.

Link to comment
Share on other sites

Re: How to....

Use mysql_fetch_object/array.

 

$SQL = sprintf("SELECT `username` FROM `users` WHERE `userid` = %u", $ID); 

/* users = users table, userid = primary key of users table, $ID = variable containing users id. */

$query = mysql_query($SQL);

$user = mysql_fetch_object($query);

echo $user->username;

 

This is very basic PHP / MySQL, if you want to create your own engine, your gonna have to know a lot more complex stuff then simple fetching.

Link to comment
Share on other sites

Re: How to....

 

Use mysql_fetch_object/array.

 

$SQL = sprintf("SELECT `username` FROM `users` WHERE `userid` = %u", $ID); 

/* users = users table, userid = primary key of users table, $ID = variable containing users id. */

$query = mysql_query($SQL);

$user = mysql_fetch_object($query);

echo $user->username;

 

This is very basic PHP / MySQL, if you want to create your own engine, your gonna have to know a lot more complex stuff then simple fetching.

I remember trying out something like that, Well I used mysql_fetch_array instead, Ill give it a mess around see if I get somewhere, Thanks Iso.

I no its pretty basic, I like to take things slow.

Link to comment
Share on other sites

Re: How to....

This is how i would do it

<?php

class User {

 function User($id) {

   $result = mysql_query("SELECT * FROM `users` WHERE `id`='".$id."'");
   $worked = mysql_fetch_array($result);

   $this->username = $worked['username'];

   // you could also format the username.

   if($this->admin == 2) {
   $this->formattedname = "<font color=brown>".$this->username."</font>";
   }
   else {
   $this->fotmattedname = $this->username;
   }

 } 

}
?>

 

an example of usage...

 

<?php
$user = new User($idUsed);
echo $user->formattedname
?>
Link to comment
Share on other sites

Re: How to....

There is different ways for doing the same thing, PHP is just this way.

You have to find your preference in what you want to use for overcoming your task, and not look at other's preferences.

In this case, extracting two rows from a database should be simple enough, but I don't see why you would want to use mysql_fetch_object to fetch it as an object? it would work, though, maybe just for syntax purposes?

All the methods below would in general, do exactly the same thing - fetch an entry from the database.

Edit, after pog-one posted: Wouldn't you want to initialize those variables used in the class first to avoid warnings, it would make sense to do so.

Edit, after Klikoka posted: It wouldn't be selecting the ID, I doubt it would be selecting anything at all.

Edit, after pgo-one posted, again: If it works, you would have to use it like this:

$varName => User(id)

User(id) => $worked

$worked => array(userid => 1, username => name, [...])

Link to comment
Share on other sites

Re: How to....

It doesn't matter guys, I was reading tutorials on websites and finally got somewhere.

I use this and also other functions:

 

function user_info($field='') {
if(empty($field))
return false;
$accepted = array('username', 'money', 'points', 'nickname', 'memberlevel', 'signature');
if(!in_array($field, $accepted))
return false;
$result = mysql_query("SELECT ". $field ." FROM members WHERE ID = ". $_SESSION['member_ID'] .";");
if(1 != mysql_num_rows($result)) :
return false;
else :
$row = mysql_fetch_assoc($result);
return $row[$field];
endif;
} 

 

Then I would put something like this in a file:

 

print user_info('username');

 

or I would do this

 

$username = user_info('username');
print "$username";
Link to comment
Share on other sites

Re: How to....

 

<?php

//You posted. when you print a variable you dont need to put it into double quotes.
$username = user_info('username');
print "$username";

// a faster way would be..
echo $username = user_info('username');
?>

 

The code seems kinda the long way around and it would only work for the user and not for others.

Whereas with the 1 i posted you could have it all run with 1 function and it would be easier to edit the whole thing :)

Link to comment
Share on other sites

Re: How to....

 

<?php

//You posted. when you print a variable you dont need to put it into double quotes.
$username = user_info('username');
print "$username";

// a faster way would be..
echo $username = user_info('username');
?>

 

The code seems kinda the long way around and it would only work for the user and not for others.

Whereas with the 1 i posted you could have it all run with 1 function and it would be easier to edit the whole thing :)

Yeah but with my code, I can create more functions, At least I no it works then I have no problem, But I'll see what I can do with your code see if it makes life a lot easier.

And my function works for all the users.

Link to comment
Share on other sites

Re: How to....

Yours dosent, it only select a feild from the table where the id = your session id.

$result = mysql_query("SELECT ". $field ." FROM members WHERE ID = ". $_SESSION['member_ID'] .";");

 

 

You could add an additional perameterfor it work with everyone

function user_info($field='', $user) {

if(empty($field))

return false;

$accepted = array('username', 'money', 'points', 'nickname', 'memberlevel', 'signature');

if(!in_array($field, $accepted))

return false;

$result = mysql_query("SELECT ". $field ." FROM members WHERE ID = ". $user .";");

if(1 != mysql_num_rows($result)) :

return false;

else :

$row = mysql_fetch_assoc($result);

return $row[$field];

endif;

}

Link to comment
Share on other sites

Re: How to....

 

$result = mysql_query("SELECT * FROM `users` WHERE `id`='".$id."'");

 

Why do people select every column from the users table? I see this all the time. Every select query selects every row in the table, even when all that is needed is an item name or gang name...

 

echo $username = user_info('username');

 

Code condensing like that may be easier and better for pog-one, but that doesn't make it better.

For instance, the order of operations there is a bit harder to understand at a glance that the other way around. I know pog-one you're going to say "that's not true".

But step back for a second, and consider that for you "that's not true", but for others, it may be entirely true.

That's really such a trivial deal, I wouldn't make anything of it. What's not trivial, is getting every single column from the users table. It's likely that if you're in the habbit of doing that sort of thing, that many more queries you've written are like that pog-one, and that's something you could really do well to optimize. ;)

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.

 Share

×
×
  • Create New...