doupian9798 2014-08-23 14:09 采纳率: 100%
浏览 25
已采纳

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

I'm having a little bit of trouble with my PHP function. In the database I get 2 results back from my query. But my function is doing something else. It changes when I edit '=' to '=='

MySQL Query:

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

Result:

ContentPagesID                  ContentTypeName
01425d4a-2abd-11e4-b991-525400  products01
014269dd-2abd-11e4-b991-525400  information01

PHP Function:

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

When I change:

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

To this:

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

It goes from showing function DisplayProducts01DesignFunction() twice to DisplayInformation01DesignFunction() once.

Any ideas on how to fix this?

  • 写回答

1条回答 默认 最新

  • duanhuanbo5225 2014-08-23 14:12
    关注

    It should actually be running it only once if your code is fixed. When you change that == to = that then doesn't remain a comparison and becomes an assignment which results to TRUE and hence your if block gets executed, which is wrong.

    if($GetAllPagesRow[ContentTypeName] = 'product01')
    

    Is not the correct way to write an if condition, if you are making your code run fine using that logic then you're doing it wrong. That = should be ==

    When you say

    if($GetAllPagesRow[ContentTypeName] = 'product01')
    

    Then that if block will always be executed even if you have 100 rows.

    Now you might ask ok when I put == then why doesn't the first if block work anymore? Its because you are comparing a wrong string, your database contains products01 and your if contains product01, see the missing s. Your actual if condition should be

    if($GetAllPagesRow[ContentTypeName] == 'products01') {
        DisplayProducts01DesignFunction();
    }
    else if ($GetAllPagesRow[ContentTypeName] == 'information01') {
        DisplayInformation01DesignFunction();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了