Browser MMO Video #5

Location of the Videos
Jony
Posts: 26
Joined: Tue May 11, 2010 3:34 pm

Re: Video#5

Post by Jony »

hardtime81 wrote:Halls whats the differenace between the two?
( {
) }
I thought it would read the script either way.
Regards,

Hardtime81
( ) are used for function parameters or for statements
[ ] are used for vectors - arrays
{ } are used to define the begining and the end of a function;



A short example:

function your_function($param1, $param2)
{
if ($param1 > $param2) echo "Number" . $param1 . "is greater than" . $param2;
else echo "Number" . $param2 . "is greater than" . $param1;
}

$var1=1;
$var2=2;

your_function($var1, $var2);

//the print will be: Number 2 is greater than 1 <-- the else
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Video#5

Post by Jackolantern »

Just to add to the explanation, { } are used to create a "block structure". A block structure creates a package of code. For example, you can only have one segment of code after an IF statement. However, by using { } braces, you can add more than one line of code after an IF statement. Also, code blocks have their own "scope". That means that any variables you declare inside a { } block structure exist only inside those { } braces. These braces are used for many different structures: functions, classes, IF statements, looping structures, SWITCH statements, TRY-CATCH structures, etc. It is just a logical way to group code together.
The indelible lord of tl;dr
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Re: Video#5

Post by Dizuki »

I have a parse error on line 7 could someone help me find it or tell me what a parse error is. Much appreciated.

<?php
include_once 'connect.php' ;
session_start () ;

if (isset ($_post ['submit'] ) )
(
$player=$_post['player'];
$password=$_post['password'];
$player=strip_tags($player);
$password=strip_tag($password);
$password=md5($password);

$query = "select name, password from players where name='$player' and 'password'" ;
$result = mysql_query($query) or die ("Could not query players");
$result2 = mysql_query_array($result);
if ($result2)
(
$_session('player')=$player;

echo "<big> Logged in successfully<br>";
echo "<a href='battle.php'>Continue</a></big>";
)
else
(
echo "<big>Wrong username or password. <a href='login.php'>Try Again</a></big>";
)
)
?>
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Video#5

Post by Jackolantern »

A "parse error" means that something is in your code that the PHP interpreter has no idea how to understand. In this case, it appears that you are using the wrong kind of braces, as well as a couple of other errors. I have changed the brackets, and also put comments anywhere else there was an error.

Change this:

Code: Select all

<?php
include_once 'connect.php' ;
session_start () ;

if (isset ($_post ['submit'] ) )
(
$player=$_post['player'];
$password=$_post['password'];
$player=strip_tags($player);
$password=strip_tag($password);
$password=md5($password);

$query = "select name, password from players where name='$player' and 'password'" ;
$result = mysql_query($query) or die ("Could not query players");
$result2 = mysql_query_array($result);
if ($result2)
(
$_session('player')=$player;

echo "<big> Logged in successfully<br>";
echo "<a href='battle.php'>Continue</a></big>";
)
else
(
echo "<big>Wrong username or password. <a href='login.php'>Try Again</a></big>";
)
)
?>
To this:

Code: Select all

<?php
include_once 'connect.php' ;
session_start () ;

if (isset ($_post ['submit'] ) )
{
$player=$_post['player'];
$password=$_post['password'];
$player=strip_tags($player);
$password=strip_tags($password); //also missing an 's' in strip_tags
$password=md5($password);

$query = "select name, password from players where name='$player' and password='$password'" ; //although this is in the original code, it needs to be 'password=$password'
$result = mysql_query($query) or die ("Could not query players");
$result2 = mysql_query_array($result);
if ($result2)
{
$_SESSION['player']=$player; //$_SESSION is like $_POST. It needs square brackets. It is also supposed to be all caps, but I don't know if that is a deal breaker with the PHP engine.

echo "<big> Logged in successfully<br>";
echo "<a href='battle.php'>Continue</a></big>";
}
else
{
echo "<big>Wrong username or password. <a href='login.php'>Try Again</a></big>";
}
}
?>
EDIT: As far as understanding the differences between ( ) { } and [ ], see the discussion a few posts back in this thread. A couple of explanations are at the top of this page.
The indelible lord of tl;dr
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Re: Video#5

Post by Dizuki »

Thanks a lot, I'm new php, I have some html experience but that's about it. All these parse errors and stuff is all Greek to me.
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Re: Video#5

Post by Dizuki »

Ok now I'm stuck again It wont go from authenticate to battle like its suppose to, it just directs me to a blank page. Is there any other problems with that code?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Video#5

Post by Jackolantern »

I would have to know more about the problem. Does this script seem to work? Can you log in? Is it the battle.php script that is showing blank? Do you see the link to it? Do you have your database set up? It seems odd that this script would be blank with no error messages, because it starts with a conditional statement that will print to the screen for either a true or false result.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Video#5

Post by hallsofvallhalla »

when you click continue it directs you to a blank page?

show us your battle.php
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Re: Video#5

Post by Dizuki »

Ok i type in my user name and password and hit the login button and it brings me to a page that's blank, the url is stuck on localhost/tutorial/authenticate.php on a blank page where it should have the continue key. If you need to i'll post the other scripts but i don't which ones I'd need to post.
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Re: Video#5

Post by Dizuki »

Code: Select all

<?php
include_once 'connect.php';
session_start ();

if  (isset($_session['player']))
(
  $player=$session['player'];
)
else
(
   echo "Not Logged in <br><br> <a href='login.php'>Login<a/>";
   exit;
)


$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);

$playerhp = $playerinfo3['hpoints'];
$playerattack = $playerinfo3['attack'];
$playerdefense = $playerinfo3['defense'];

if (isset($_GET['creature']))
{
$creature=$_GET['creature'];
$creatureinfo="SELECT * from creatures where name = '$creature'";
$creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you were fighting!");
$creatureinfo3=mysql_fetch_array($creatureinfo2);

}
else
{
$creatureinfo="SELECT * from creatures order by rand() limit 1";
$creatureinfo2=mysql_query($creatureinfo) or die("could get a creature!");
$creatureinfo3=mysql_fetch_array($creatureinfo2);
}

$creature = $creatureinfo3['name'];
$creaturehp = $creatureinfo3['hpoints'];
$creatureattack = $creatureinfo3['attack'];
$creaturedefense = $creatureinfo3['defense'];

/////player info
echo "<u> " . $playerinfo3['name'] . "</u><br>";
echo "Hit points = " . $playerhp . "<br>";
echo "Attack = " . $playerattack . "<br>";
echo "Defense = " . $playerdefense . "<br><br><br>";

///////creature info
echo "<u> " . $creatureinfo3['name'] . "</u><br>";
echo "Hit points = " . $creaturehp . "<br>";
echo "Attack = " . $creatureattack . "<br>";
echo "Defense = " . $creaturedefense . "<br><br><br>";

echo "<a href='attack.php?creature=$creature'>Attack!";

?>
Post Reply

Return to “Older Browser MMO Videos”