Jump to content


Photo

Please review this code


  • Please log in to reply
9 replies to this topic

#1 jokeascool

jokeascool
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 17 May 2006 - 03:03 PM

Hello all,

I am new to php and I am just playing around at this point.

I am trying to right a simple login script and I am getting some parse errors.

Could you please review the following code and give any suggestions.


<?php

$host="localhost";
$user="jkaskel";
$password="tasha1";
$database="bfcc";

$uname=$_Post['username'];
$pword=$_Post['password'];




$connection=mysql_connect($host,$user,$password)
or die ("Problems Connecting to Server");

$db = mysql_select_db($database,$connection)
or die ("Could not create connection with the database");


$valid=0;

$query="Select * from users where username='$uname'";

$result=mysql_query($query)
or die("Could not execute query")


[!--coloro:#3366FF--][span style=\"color:#3366FF\"][!--/coloro--]$row = mysql_fetch_array($result);[!--colorc--][/span][!--/colorc--]***this line is giving a parse error.
extract($row);

if($pword == $password)
{
header("location:loginok.php");
}


else
{
header("location:index.php");
}

?>


Thank you for your help and support!

Joe



#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 17 May 2006 - 03:09 PM

welcome to the forums! hope you find lots of great help here

you're missing the semicolon on the line before:
$result=mysql_query($query)
or die("Could not execute query")

good luck!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 jokeascool

jokeascool
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 17 May 2006 - 04:08 PM

[!--quoteo(post=374680:date=May 17 2006, 11:09 AM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ May 17 2006, 11:09 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
welcome to the forums! hope you find lots of great help here

you're missing the semicolon on the line before:
$result=mysql_query($query)
or die("Could not execute query")

good luck!
[/quote]

Thank you very much..

Still getting parse error however.

See anything else that stands out a wrong or bad coding?

Thanks

Joe


#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 17 May 2006 - 04:34 PM

[!--quoteo(post=374702:date=May 17 2006, 12:08 PM:name=jokeascool)--][div class=\'quotetop\']QUOTE(jokeascool @ May 17 2006, 12:08 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Thank you very much..

Still getting parse error however.

See anything else that stands out a wrong or bad coding?

Thanks

Joe
[/quote]

why don't you print out your error here as well as double check to make sure you've got ALL your code listed.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 jokeascool

jokeascool
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 17 May 2006 - 07:29 PM

[!--quoteo(post=374716:date=May 17 2006, 12:34 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ May 17 2006, 12:34 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
why don't you print out your error here as well as double check to make sure you've got ALL your code listed.
[/quote]

Here is the error I am Recieving:
Parse error: syntax error, unexpected T_VARIABLE in V:\Core\htdocs\login.php on line 29


Thanks

Joe

P.S. That was all of the code.. I was trying to test that much as a simple program and then expand from those fundementals.


#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 17 May 2006 - 07:56 PM

[!--quoteo(post=374766:date=May 17 2006, 03:29 PM:name=jokeascool)--][div class=\'quotetop\']QUOTE(jokeascool @ May 17 2006, 03:29 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Here is the error I am Recieving:
Parse error: syntax error, unexpected T_VARIABLE in V:\Core\htdocs\login.php on line 29
Thanks

Joe

P.S. That was all of the code.. I was trying to test that much as a simple program and then expand from those fundementals.
[/quote]

and you added the semicolon i showed you above? the final code should be:
<?php

$host="localhost";
$user="jkaskel";
$password="tasha1";
$database="bfcc";

$uname=$_Post['username'];
$pword=$_Post['password'];




$connection=mysql_connect($host,$user,$password)
or die ("Problems Connecting to Server");

$db = mysql_select_db($database,$connection)
or die ("Could not create connection with the database");


$valid=0;

$query="Select * from users where username='$uname'";

$result=mysql_query($query)
or die("Could not execute query");


$row = mysql_fetch_array($result);
extract($row);

if($pword == $password)
{
header("location:loginok.php");
}


else
{
header("location:index.php");
}

?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 jokeascool

jokeascool
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 17 May 2006 - 08:23 PM

Here is the code as it stands. I added the extra semi colon, plus a line to see if the query comes back null.

Let me know what you think.

<?php

$host="localhost";
$user="jkaskel";
$password="tasha1";
$database="bfcc";

$uname=$_Post['username'];
$pword=$_Post['password'];




$connection=mysql_connect($host,$user,$password)
            or die ("Problems Connecting to Server");
            
$db = mysql_select_db($database,$connection)
            or die ("Could not create connection with the database");
                        

$valid=0;

$query="Select * from users where username='$uname'";

$result=mysql_query($query)
            or die("Could not execute query");


$row = mysql_fetch_array($result);

extract($row);

If($row==0)
{
  echo: "No records found";
}

if($pword == $password)
{
  header("location:loginok.php");
}


else
{
  header("location:index.php");
}

?>

Thanks

Joe

#8 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 17 May 2006 - 09:26 PM

If($row==0)
{
  echo: "No records found";
}

Here, $row is an array, so it will never "equal" to zero. The following is how you should do it.
If(count($row)==0)
{
  echo: "No records found";
}

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#9 jokeascool

jokeascool
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 May 2006 - 03:49 PM

[!--quoteo(post=374790:date=May 17 2006, 05:26 PM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ May 17 2006, 05:26 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If($row==0)
{
  echo: "No records found";
}

Here, $row is an array, so it will never "equal" to zero. The following is how you should do it.
If(count($row)==0)
{
  echo: "No records found";
}
[/quote]

Could someone please explain briefly how PHP interpets code.

Does it do so line by line or some way else.

The reason I ask is to be able to trouble shoot if it is doing it logically the error will be in line 29. However anyway else the error could be anywhere and just triggered by some action taken by line 29.

I guess some newbie trouble shooting tips would be helpful.

Thanks

Joe

PS. Sorry for the crappy spelling and punctuation. Nature calls.

HEHE

#10 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 18 May 2006 - 08:18 PM

When asking for help to resolve an error, post the error so the members can help. You should have been getting a "parse error"?

Remove the ":" following the "echo".
echo: "No records found";
ie
echo "No records found";
When checking if records have been returned, use [a href=\"http://www.php.net/mysql_num_rows\" target=\"_blank\"]mysql_num_rows[/a].
eg:
$result = mysql_query($query);
if (!mysql_num_rows($result))
{
    print 'No records found';



}
else
{
    etc etc
}

I guess some newbie trouble shooting tips would be helpful.

Most troubleshooting skills will probably come from experience, for now know that if you get a "parse error", it's likely that the cause of it is a couple of lines above the line that the error is reported to be on. You'll have to use your head to parse the script checking that all lines contain valid PHP code. Look for missing semicolons, unclosed brackets etc.

Many times the error tells you exactly what is wrong, always assume that PHP is right and you're wrong.

If the script isn't doing what you expect, then find out what the first point is that the script isn't doing what you expect by echoing variables (you can use [a href=\"http://www.php.net/print_r\" target=\"_blank\"]print_r[/a] for arrays. [a href=\"http://www.php.net/var_dump\" target=\"_blank\"]var_dump[/a] is also useful at times) to make sure they have the value you expect througout the script.

Some [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=54859\" target=\"_blank\"]PHP Editors[/a] may also help you find errors more easily.

If anyone has more tips or is willing to give more comprehensive examples of general troubleshooting, feel free to post them here.

You should also find the phpfreaks [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=31047\" target=\"_blank\"]PHP FAQ[/a] helpful when dealing with specific issues.

EDIT: I notice that you did actually post the error, so I was wrong saying that you hadn't. The error I got when copying and pasting your code was different however. Ensure that the code you posted here is exactly the same as the one you're using locally.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users