0

I seem to be having a rough time with this snippet of code, Which i dont understand because on my home server this works 100% perfect. basically this script takes the users ip and stores it into a mysql table. every time a user posts it checks the table to see if the ip has already posted. when i run mysql_error() on num_rows which apears to be the problem, i get:

Parse error: syntax error, unexpected T_LOGICAL_OR on line 119

Any ideas?

php:

$poster_ip=$_SERVER['REMOTE_ADDR'];//Posters ip
//check for ip double posting
//selecet ip from table
$sql="SELECT * FROM $tbl_name WHERE ip='$poster_ip'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
or die mysql_error();//line 119
//if result matche posterip, table row must be 1
if($count==1){
//ip taken
echo "This IP has already submited a post. You may not submit another.";
exit();
//else script continues
}
4
  • The answer given is perfect, but I would probably also try to move away from the terrible or die pattern. AND: it's also time for you to stop using mysql. It's way old and deprecated. People use PDO or mysqli these days. Lastly: there's a SQL injection bug in your code. Commented Jan 4, 2013 at 14:51
  • is there really? id be interested in knowing where. Commented Jan 4, 2013 at 14:56
  • 1
    Nice SQL injection hole. Enjoy having your server pwn3d. Commented Jan 4, 2013 at 15:14
  • Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Jan 4, 2013 at 15:24

7 Answers 7

14

Remove the semicolon because that terminates the statement and the or die is treated as a new statement, causing the error:

$count=mysql_num_rows($result) or die(mysql_error());
                         //   ^ no semicolon

You also need parenthesis around the die() call.

Side note: or die(mysql_error()) is not considered good practice because it's difficult to maintain between development and production environments. or trigger_error(mysql_error()) would be better - this writes to your error log. Also consider upgrading to PDO or MySQLi because this MySQL library is deprecated and discouraged.

Sign up to request clarification or add additional context in comments.

Comments

1

Remove semicolon!

$count=mysql_num_rows($result);
------------------------------^
or die mysql_error();//line 119

Change it to:

$count=mysql_num_rows($result) or die mysql_error();

It terminates!!!

Suggestion:

Do not use mysql_* functions as they are deprecated. Use mysqli or PDO instead.

1 Comment

+1 reciprocated, don't forget it also needs () around the die call :)
0
$count=mysql_num_rows($result);
or die mysql_error();

should be

$count=mysql_num_rows($result) or die mysql_error();

Any, I suggest you to avoid the use of mysql_* as they are being deprecated. Use mysqli or PDO instead.

Comments

0

Try changing this:

$count=mysql_num_rows($result);
or die mysql_error();//line 119

To this:

$count=mysql_num_rows($result) or die mysql_error();//line 119

Comments

0

Please remove semicolon and add () to die in order to show it is errors & then die

$count=mysql_num_rows($result) or die(mysql_error()); //line 119

Comments

0

you can't begin the line with "or" In line 118 you end with ";"

$count = mysql_num_rows($result) or die mysql_error();

But it would be better to to the error checking after result:

if(!$result) {
    die mysql_error();        
}

Comments

0

This code will work. i had removed the semicolon(;) before 'or die' which caused the error..

$poster_ip=$_SERVER['REMOTE_ADDR'];//Posters ip
//check for ip double posting
//selecet ip from table
$sql="SELECT * FROM $tbl_name WHERE ip='$poster_ip'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result) or die mysql_error();//line 119
//if result matche posterip, table row must be 1
if($count==1){
//ip taken
echo "This IP has already submited a post. You may not submit another.";
exit();
//else script continues
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.