Results 1 to 4 of 4

Thread: Why doesn't this Boolean work?

  1. #1

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    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;


      } 

  2. #2
    Junior Member
    Join Date
    Nov 2007
    Posts
    22

    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

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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'
    Last edited by penagate; Dec 12th, 2008 at 03:49 AM.

  4. #4

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width