简体   繁体   English

如果语句或while循环在php中失败了

[英]If statement or while loop failing on me in php

I am fairly new to coding in php and have come accross a bit of a problem which I think is very simple. 我是一个相当新的PHP编码,并遇到了一个问题,我觉得很简单。 I am trying to capture the IP address of anybody that visits my website and stores that in my database. 我试图捕获访问我的网站并将其存储在我的数据库中的任何人的IP地址。 The code is as follows, I am not even sure the code will work but I do get the following error Parse error: syntax error, unexpected T_INC on line 29 which I will highlight in the code: 代码如下,我甚至不确定代码是否会工作,但我确实得到以下错误解析错误:语法错误,第29行意外的T_INC,我将在代码中突出显示:

$ip = $_SERVER['HTTP_CLIENT_IP']; 
                $query="SELECT * FROM ip";
                $result = mysql_query($query);
                $num = mysql_numrows($result);
                $i = 1;
                $found=false;
                while(($i - 1) < $num){
                    $selection = mysql_query("SELECT ip FROM ip WHERE id=$i");
                    $tip = mysql_fetch_assoc($selection);
                    if($tip == $ip){
                        $found = true;
                    }
                i++; //This is line 29
                }
                if($found == false){
                    $sql = "INSERT INTO `rowley_blog`.`ip` (`ip`) VALUES ('$ip');";
                    mysql_query($sql);
                    mysql_close();
                }

您需要使用前缀变量$ ,所以它的$i++

它应该是这样的:

$i++;

你需要在所有变量之前加上一个美元符号 - 应该是这样

$i++;

Apart from the syntax error: This code seems blown-up to me. 除了语法错误:这段代码似乎让我感到震惊。 Doesn't this do the same: 这不是一样的:

$ip = $_SERVER['HTTP_CLIENT_IP'];
mysql_query("REPLACE INTO `ip` (`ip`) VALUES ('" . mysql_real_escape_string($ip) . "')");

Remember: Good code is short code ;) 记住:好的代码是短代码;)

Furthermore: If you are only beginning to code PHP, please don't even start using the mysql_ extension. 此外:如果您只是开始编写PHP代码,请不要开始使用mysql_扩展。 Instead use PDO : 而是使用PDO

$ip = $_SERVER['HTTP_CLIENT_IP'];
$db->query("REPLACE INTO `ip` (`ip`) VALUES ('" .$db->quote($ip) . "')");

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

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