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