Why doesn't this Boolean work?
I have never had a boolean work in php although i havet only tinkered.
I am putting 8 records filled with html into a mysql table, w1----w8 .
When the webpage loads i only need retrieve the first record (w1) and then echo it, the others(w2--w8) are controlled by the <form method = "post" dependng if values are posted or not. So if values are posted then (w2---w8) are echo'ed. w1 contains the header html with the css link for the webpage hence it sets the main layout. Whereas w2----w8 are a noticeboard (or forum) like where values are being appended into the page.
So i though i'd use a Boolean to make the first record only display once, i.e when the page loads at first, i tried this (but this doesn't work, every time i "<form post" w1 still gets echo'ed.......?
PHP Code:
if ($foo == True) {
mysql_select_db("webinfo", $con);
$sql="SELECT w1 FROM webdata";
if ($result=mysql_query($sql)) {
$foo = False;
echo $result;
}
Re: Why doesn't this Boolean work?
I've had issues with booleans as well; I've had fairly good luck using the identical test though ===. I typically use something like
Code:
$result = mysql_query($sql);
if(!$result) {
// oops an error occured
}
I haven't tried this, but it *may* work
Code:
if(($result = mysql_query($sql)) !== false) {
$foo = false;
echo $result;
}
Shouldn't $result be either a resource id or false on an error though? I'd think you'd need to use mysql_fetch_assoc($result) or something... I'll have to try that way sometime.
Hope that helps
Re: Why doesn't this Boolean work?
Suggestions:
1. Indent code properly
2. 'true', not 'True'
3. Use the is_resource function to check if a libmysql query was successful, or !== false as shown above. (== performs an equivalence test; === performs an equality (or 'identity') test. Because PHP uses loose typing, you should always consider whether or not the type of the variable is relevant.)
4. 'false', not 'False'
Re: Why doesn't this Boolean work?
Quote:
Originally Posted by penagate
Suggestions:
1. Indent code properly
2. 'true', not 'True'
3. Use the is_resource function to check if a libmysql query was successful, or !== false as shown above. (== performs an equivalence test; === performs an equality (or 'identity') test. Because PHP uses loose typing, you should always consider whether or not the type of the variable is relevant.)
4. 'false', not 'False'
I was just following the offical documentation,
http://www.php.net/manual/en/language.types.boolean.php