Joel Posted August 7, 2008 Share Posted August 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
Guest Anonymous Posted August 7, 2008 Share Posted August 7, 2008 Re: How to.... Read the MySQL tutorial... http://www.tizag.com/mysqlTutorial/ http://dev.mysql.com/doc/refman/5.0/en/tutorial.html http://www.php-mysql-tutorial.com/ You'll need to study such things if your planning on creating your own game engine or you'll get no were fast. Quote Link to comment Share on other sites More sharing options...
Joel Posted August 7, 2008 Author Share Posted August 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
Isomerizer Posted August 7, 2008 Share Posted August 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
Joel Posted August 7, 2008 Author Share Posted August 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
Klikoka Posted August 9, 2008 Share Posted August 9, 2008 Re: How to.... This is what i am using at the moment. $sql = "SELECT * FROM TABLENAMEHERE"; $query = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_object($query); $name = htmlspecialchars($row->username); Quote Link to comment Share on other sites More sharing options...
POG1 Posted August 9, 2008 Share Posted August 9, 2008 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 ?> Quote Link to comment Share on other sites More sharing options...
Klikoka Posted August 9, 2008 Share Posted August 9, 2008 Re: How to.... With your one Pog where would it be selecting the ID from to select the username? Quote Link to comment Share on other sites More sharing options...
POG1 Posted August 9, 2008 Share Posted August 9, 2008 Re: How to.... You could use it anywhere 8-) Just set the variable in the perameters and the function does the rest for you.. $varName = new User($_GET['id']); Quote Link to comment Share on other sites More sharing options...
Spudinski Posted August 9, 2008 Share Posted August 9, 2008 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, [...]) Quote Link to comment Share on other sites More sharing options...
Klikoka Posted August 9, 2008 Share Posted August 9, 2008 Re: How to.... Ahh, Ok. Quote Link to comment Share on other sites More sharing options...
Joel Posted August 9, 2008 Author Share Posted August 9, 2008 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"; Quote Link to comment Share on other sites More sharing options...
POG1 Posted August 9, 2008 Share Posted August 9, 2008 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 :) Quote Link to comment Share on other sites More sharing options...
Joel Posted August 9, 2008 Author Share Posted August 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
POG1 Posted August 9, 2008 Share Posted August 9, 2008 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; } Quote Link to comment Share on other sites More sharing options...
Floydian Posted August 13, 2008 Share Posted August 13, 2008 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. ;) Quote Link to comment Share on other sites More sharing options...
POG1 Posted August 20, 2008 Share Posted August 20, 2008 Re: How to.... Yeah true to only select whats needed but if you want to select a field which isn't selected in a array you will need to write another selection query.. Quote Link to comment Share on other sites More sharing options...
Isomerizer Posted August 20, 2008 Share Posted August 20, 2008 Re: How to.... Yeah true to only select whats needed but if you want to select a field which isn't selected in a array you will need to write another selection query.. Or you could just add ", `extra_field`" to the cols your selecting in the query? Quote Link to comment Share on other sites More sharing options...
POG1 Posted August 20, 2008 Share Posted August 20, 2008 Re: How to.... :mrgreen: Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.