Jump to content

Please review this code


jokeascool

Recommended Posts

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

Link to comment
Share on other sites

[!--quoteo(post=374680:date=May 17 2006, 11:09 AM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ May 17 2006, 11:09 AM) [snapback]374680[/snapback][/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:
[code]
$result=mysql_query($query)
or die("Could not execute query")
[/code]

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
Link to comment
Share on other sites

[!--quoteo(post=374702:date=May 17 2006, 12:08 PM:name=jokeascool)--][div class=\'quotetop\']QUOTE(jokeascool @ May 17 2006, 12:08 PM) [snapback]374702[/snapback][/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.
Link to comment
Share on other sites

[!--quoteo(post=374716:date=May 17 2006, 12:34 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ May 17 2006, 12:34 PM) [snapback]374716[/snapback][/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.
Link to comment
Share on other sites

[!--quoteo(post=374766:date=May 17 2006, 03:29 PM:name=jokeascool)--][div class=\'quotetop\']QUOTE(jokeascool @ May 17 2006, 03:29 PM) [snapback]374766[/snapback][/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:
[code]
<?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");
}

?>
[/code]
Link to comment
Share on other sites

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.

[code]<?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");
}

?>
[/code]

Thanks

Joe
Link to comment
Share on other sites

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

Here, $row is an array, so it will never "equal" to zero. The following is how you should do it.
[code]If(count($row)==0)
{
  echo: "No records found";
}[/code]
[/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
Link to comment
Share on other sites

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".
[code]
echo: "No records found";
[/code]
ie
[code]
echo "No records found";
[/code]
When checking if records have been returned, use [a href=\"http://www.php.net/mysql_num_rows\" target=\"_blank\"]mysql_num_rows[/a].
eg:
[code]
$result = mysql_query($query);
if (!mysql_num_rows($result))
{
    print 'No records found';



}
else
{
    etc etc
}
[/code]

[quote]
I guess some newbie trouble shooting tips would be helpful.
[/quote]
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 [b]general troubleshooting[/b], 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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.