PHP While语句显示与queryresult相同的结果

PHP While语句显示与queryresult相同的结果,php,mysql,function,if-statement,Php,Mysql,Function,If Statement,我的PHP函数有点问题。在数据库中,我从查询中得到2个结果。但我的职能是做其他事情。当我将“=”编辑为“=”时,它会更改 MySQL查询: SELECT ContentPages.ContentPagesID, ContentType.ContentTypeName FROM `ContentPages` INNER JOIN ContentType ON ContentPages.ContentTypeID = ContentType.ContentTypeID INNER JOIN Cont

我的PHP函数有点问题。在数据库中,我从查询中得到2个结果。但我的职能是做其他事情。当我将“=”编辑为“=”时,它会更改

MySQL查询:

SELECT ContentPages.ContentPagesID, ContentType.ContentTypeName
FROM `ContentPages` INNER JOIN ContentType ON ContentPages.ContentTypeID = ContentType.ContentTypeID INNER JOIN ContentInformation ON ContentPages.ContentInformationID = ContentInformation.ContentInformationID
结果:

ContentPagesID                  ContentTypeName
01425d4a-2abd-11e4-b991-525400  products01
014269dd-2abd-11e4-b991-525400  information01
PHP函数:

function GetAllPages() {
$GetAllPagesSql = "SELECT ContentPages.ContentPagesID, ContentType.ContentTypeName FROM `ContentPages` INNER JOIN ContentType ON ContentPages.ContentTypeID = ContentType.ContentTypeID INNER JOIN ContentInformation ON ContentPages.ContentInformationID = ContentInformation.ContentInformationID";
    $GetAllPagesQuery = mysql_query($GetAllPagesSql);
    while (($GetAllPagesRow = \mysql_fetch_array($GetAllPagesQuery)) != false) {
        if($GetAllPagesRow[ContentTypeName] === 'product01') {
            DisplayProducts01DesignFunction();
        }
        else if ($GetAllPagesRow[ContentTypeName] === 'information01') {
            DisplayInformation01DesignFunction();
        }
    }
}
当我改变时:

if($GetAllPagesRow[ContentTypeName] = 'product01') {
    DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] = 'information01') {
    DisplayInformation01DesignFunction();
}
为此:

if($GetAllPagesRow[ContentTypeName] === 'product01') {
    DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] === 'information01') {
    DisplayInformation01DesignFunction();
}
从两次显示函数DisplayProducts01设计函数到一次显示信息01设计函数


关于如何修复这个问题有什么想法吗?

如果您的代码已经修复,它实际上应该只运行一次。当您将==更改为=时,它就不再是一个比较,而是一个赋值,结果为TRUE,因此执行if块,这是错误的

if($GetAllPagesRow[ContentTypeName] = 'product01')
不是编写if条件的正确方法,如果您使用该逻辑使代码运行良好,那么您就做错了。那应该是==

当你说

if($GetAllPagesRow[ContentTypeName] = 'product01')
然后,即使有100行,if块也将始终执行

现在当我放置==时,您可能会问ok,那么为什么第一个if块不再起作用?这是因为您正在比较错误的字符串,您的数据库包含products01,如果您的数据库包含product01,请参阅缺少的s。你的实际情况应该是

if($GetAllPagesRow[ContentTypeName] == 'products01') {
    DisplayProducts01DesignFunction();
}
else if ($GetAllPagesRow[ContentTypeName] == 'information01') {
    DisplayInformation01DesignFunction();
}

=是赋值运算符==是相等运算符,==是您不需要的绝对相等运算符。使用==