Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回的函数结果在条件语句中混淆_Php - Fatal编程技术网

Php 返回的函数结果在条件语句中混淆

Php 返回的函数结果在条件语句中混淆,php,Php,我有一个用PHP处理数据库的类。此类中的Add函数是: function Add($post_id) { if(!is_numeric($post_id)) { return 1181; } $query = "..."; $result = mysql_query($query, $this->link); return $result; }

我有一个用PHP处理数据库的类。此类中的Add函数是:

    function Add($post_id)
    {
        if(!is_numeric($post_id))
        {
            return 1181;
        }

        $query  = "...";
        $result = mysql_query($query, $this->link);
        return $result;
    }
我还有一个页面,它获取表单数据并将它们传递给这个类。页面代码为:

$result = $obj->Add($post_id);

if($result == 1181)
{
    echo 'invalid';
}
else if($result)
{
    echo 'success';
}
else
{
    echo 'error';
}
返回值为1,输出必须为“success”,但我收到“invalid”消息。如果我交换'invalid'和'success'条件语句,一切都很好,但我想知道这是为什么

变量转储($result)是一个很好的起点。在布尔上下文中,1181将转换为
true
,因此不要因为它打印了
success
,就期望它成功

您可能传递了错误的
post\u id
。启用显示警告和通知。不要使用疯狂的魔法常量,使用
false
或抛出异常。始终检查mysql\u query的返回值


如果你这样做了,我就不必猜测,你可以取得进展并提出有意义的问题。

就像其他人在评论中指出的那样,你应该在这些类型的情况下使用。这里有一个例子

function Add($post_id)
{
    if(!is_numeric($post_id))
    {
        throw new InvalidArgumentException( 'Argument should be numeric' );
    }

    $query  = "...";
    $result = mysql_query($query, $this->link); 
    return $result;
}

try
{
    $result = $obj->Add($post_id);
}
catch( InvalidArgumentException $e )
{
    /* handle the invalid argument exception */
}

if($result)
{
    echo 'success';
}
else
{
    echo 'error';
}
此外,如果您坚持对错误使用代码,您可以使用以下方法:

function Add($post_id)
{
    if(!is_numeric($post_id))
    {
        throw new InvalidArgumentException( 'Argument should be numeric', 1181 );
    }

    if($post_id <= 0)
    {
        throw new InvalidArgumentException( 'Argument should be larger than 0', 1182 );
    }

    $query  = "...";
    $result = mysql_query($query, $this->link); 
    return $result;
}

try
{
    $result = $obj->Add($post_id);
}
catch( InvalidArgumentException $e )
{
    switch( $e->getCode() )
    {
        case 1181:
            /* handle the invalid argument exception with code 1181 */
            break;
        case 1182:
            /* handle the invalid argument exception with code 1182 */
            break;
        default:
            /* handle other invalid argument exceptions */
            break;
    }
}
函数添加($post\u id)
{
如果(!是数字($post\u id))
{
抛出新的InvalidArgumentException('参数应为数字',1181);
}
如果($post_id link);
返回$result;
}
尝试
{
$result=$obj->Add($post_id);
}
捕获量(无效argumentexception$e)
{
开关($e->getCode())
{
判例1181:
/*处理代码为1181的无效参数异常*/
打破
判例1182:
/*处理代码为1182的无效参数异常*/
打破
违约:
/*处理其他无效参数异常*/
打破
}
}

最后,就像其他人也评论过的一样,异常处理与SQL注入无关,也不会妨碍SQL注入。

输入是什么?如果我们不知道
$post\u id
是什么,我们该如何推断问题?在我看来,您应该使用异常,而不是返回像“1181”这样的数字。这个数字是什么意思?@Tomalak Geret'kal:
$post\u id
是一个数字值。我没有使用from Exceptions,因为我想阻止sql注入,在检查
$post\u id
之后,我还有一些其他代码。一切正常,但我不知道这是什么问题……我看不到异常和SQL注入之间的相关性。@新手:异常和SQL注入是完全正交的。“我没有从异常中使用,因为我想阻止sql注入”毫无意义。告诉我们
$post_id
的确切具体值,而不是给我们一些毫无意义的含糊不清的废话。这是目前这个问题可能的最佳答案。1181是真的,但我用相等运算符检查它。此条件为false,因为返回值为1。好吧我是从数字中使用的,因为我在这个方法中有很多错误数字,比如“无效电子邮件”。我不能用错误的值显示所有这些。当然我把它改成了使用Exceptions@Novice:assert
var\u dump(true==1181)
。这个答案是正确的。如果希望得到预期的结果,应该使用严格相等运算符
var\u dump(true==1181)
var\u dump($result)