簡體   English   中英

mysql_num_rows()錯誤

[英]mysql_num_rows() error

我似乎對這段代碼很不滿意,我不明白,因為在我的家庭服務器上,這是100%完美的。 基本上,此腳本使用用戶ip並將其存儲到mysql表中。 每次用戶發布時,它都會檢查表以查看IP是否已經發布。 當我在num_rows上運行mysql_error()時,這是問題所在,我得到:

Parse error: syntax error, unexpected T_LOGICAL_OR on line 119

有任何想法嗎?

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

刪除分號,因為這會終止該語句,並且or die被視為新語句,從而導致錯誤:

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

您還需要在die()調用前后加上括號。

旁注: or die(mysql_error())被認為不是好習慣,因為在開發和生產環境之間很難維護。 or trigger_error(mysql_error())會更好-這會寫入您的錯誤日志。 還考慮升級到PDOMySQLi,因為不建議使用此MySQL庫。

刪除分號!

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

更改為:

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

它終止了!!!

建議:

不要使用mysql_*函數,因為它們已被棄用。 改用mysqliPDO

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

應該

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

任何,我建議您避免使用mysql_*因為它們已被棄用。 改用mysqliPDO

嘗試更改此:

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

對此:

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

請刪除分號添加()到死,以顯示它是錯誤然后死

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

您不能以“或”開始該行。在第118行中,您必須以“;”結束

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

但是最好對結果后的錯誤進行檢查:

if(!$result) {
    die mysql_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