Page 1 of 13

Browser MMO Video#2

Posted: Mon May 25, 2009 3:40 pm
by hallsofvallhalla
second video, how to build the database and enter some data. Also building the connect.php. Post any Q and A and comments in this thread.

[vimeo]http://vimeo.com/4828407[/vimeo]

the video is in HD but vimeo only allows 1 HD a week, so it wont be for now, i will find an alternative or next week upload it in HD. I zoom in to all the coding so you dont need HD.


source

connect.php

Code: Select all

<?php

$db = mysql_connect("localhost","root","") or die("Could not connect to db");
if(!$db)
 die("no db");
if(!mysql_select_db("tutorial",$db))
 die("No Database Selected");
 
 ?>
test.php

Code: Select all

<?php
include 'connect.php';

$playerinfo ="select * from players where name = 'player1'";
$playerinfo2 = mysql_query($playerinfo) or die ("could not select players");
$playerinfo3 = mysql_fetch_array($playerinfo2);

echo "Player1's email is " . $playerinfo3['email'];
echo "<br>Player1's id is " . $playerinfo3['id'];

?>

Re: Video#2

Posted: Mon May 25, 2009 10:28 pm
by Falken
Good tutorials I must say even tho I know php quite well :P

Some details that is good to mention. In your code if there is 2 players called "player1" it will only return one of them or in some cases throw an error.
To fix this either loop the array and echo all players or add " LIMIT 0, 1" in the end of the sql string, that way it will only retrieve the first player1 row found in the database. You could also use mysql_fetch_row instead of array if you just want one row too

Re: Video#2

Posted: Mon May 25, 2009 11:11 pm
by hallsofvallhalla
thanks Falken. I have seen the results of your PHP coding so I value your opinion very much.

Thats why I always use auto increment id. I query it and there is no chance for pulling 2 of the same. ;) For demo purposes I used name. THough I do use limit one much when i am using a inventory system, help keep from deleting or moving 2 of the same item.

Re: Video#2

Posted: Tue May 26, 2009 8:05 am
by Falken
Ye using ID is the best thing. Altho if all usernames has to be unique tho, there is no reason why you couldn't remove the id and set username as primary key instead.

Re: Video#2

Posted: Tue May 26, 2009 1:51 pm
by hallsofvallhalla
that is true, my main reason for it is so I always have a count of how many players there are without having to to do a for loop with a query and because I often separate skills, stats, inventory, ect to other tables and I link them by a id, then the skills and stats tables are non numerical character free and use less space and pull faster. Guess its just a habit.

Re: Video#2

Posted: Tue May 26, 2009 2:29 pm
by Raven67854
Nice job on the tutorial halls.

Re: Video#2

Posted: Tue May 26, 2009 10:28 pm
by Falken
ok!
hallsofvallhalla wrote:that is true, my main reason for it is so I always have a count of how many players there are without having to to do a for loop with a query.
A little hint for the future if you need something like that. (Like if the player with id 3 is removed, then the last id of lets say 23 wont be correct ;) )
You can easily use the following sql query to count rows:

Code: Select all

SELECT COUNT(*) AS myCount FROM myTable;
or in php:

Code: Select all

$sql = "SELECT * FROM myTable";
$count = mysql_num_rows($result);
that returns the number of rows that the query got. Very useful :)

Re: Video#2

Posted: Wed May 27, 2009 3:21 am
by kyraneth
you two really know your codes don't you :D I still haven't got to check the vids, i'll probably do it in the afternoon.

Re: Video#2

Posted: Wed May 27, 2009 4:13 am
by hallsofvallhalla
check the vids!!!! heeh i am still waiting to for people to choose what this game is about. IF I don't get answers soon I will choose myself.

Re: Video#2

Posted: Sun Jun 14, 2009 10:18 pm
by Socapex
Thank you very much for the vids, I'm liking it alot! I'm presently following on mac with MAMP and textmate, and after small tweaks it works fine. I'll be posting up anything that mac users might bump into. As for this video (and the first)...

When connecting to MAMP on your browser, use "localhost:8888" instead of just "localhost".

If you ever bump into the error (when running the script either in textmate or what-not) that resembles: Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2)

You'll have to create a symbolic link to "/tmp/mysql/mysql.sock" in your var folder...
Launch your terminal,
> cd ..
> cd ..
> cd /var
> sudo mkdir mysql
>cd ..
> sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

There, everything works fine :)
Socapex