简体   繁体   English

mysql_num_rows()错误

[英]mysql_num_rows() error

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. 我似乎对这段代码很不满意,我不明白,因为在我的家庭服务器上,这是100%完美的。 basically this script takes the users ip and stores it into a mysql table. 基本上,此脚本使用用户ip并将其存储到mysql表中。 every time a user posts it checks the table to see if the ip has already posted. 每次用户发布时,它都会检查表以查看IP是否已经发布。 when i run mysql_error() on num_rows which apears to be the problem, i get: 当我在num_rows上运行mysql_error()时,这是问题所在,我得到:

Parse error: syntax error, unexpected T_LOGICAL_OR on line 119

Any ideas? 有任何想法吗?

php: 的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
}

Remove the semicolon because that terminates the statement and the or die is treated as a new statement, causing the error: 删除分号,因为这会终止该语句,并且or die被视为新语句,从而导致错误:

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

You also need parenthesis around the die() call. 您还需要在die()调用前后加上括号。

Side note: or die(mysql_error()) is not considered good practice because it's difficult to maintain between development and production environments. 旁注: or die(mysql_error())被认为不是好习惯,因为在开发和生产环境之间很难维护。 or trigger_error(mysql_error()) would be better - this writes to your error log. or trigger_error(mysql_error())会更好-这会写入您的错误日志。 Also consider upgrading to PDO or MySQLi because this MySQL library is deprecated and discouraged. 还考虑升级到PDOMySQLi,因为不建议使用此MySQL库。

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. 不要使用mysql_*函数,因为它们已被弃用。 Use mysqli or PDO instead. 改用mysqliPDO

$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. 任何,我建议您避免使用mysql_*因为它们已被弃用。 Use mysqli or PDO instead. 改用mysqliPDO

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

请删除分号添加()到死,以显示它是错误然后死

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

you can't begin the line with "or" In line 118 you end with ";" 您不能以“或”开始该行。在第118行中,您必须以“;”结束

$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();        
}

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM