trvo Posted April 10, 2007 Share Posted April 10, 2007 Hi Guys, I'm new here and looking for help with an application that I am developing. Basically my application collates data over several pages of forms in an ini file and then puts it in the mysql database. I have some Yes / No radio buttons on some of these forms which populate the .ini file with the word Yes or No. So up to this stage it all works fine. Now, when I retrieve the data php, for some reason, converts Yes or No into a boolean value. It also does this when retrieving the data back from mysql after I have written the word 'Yes' / 'No' to the database. I can over come this each time I reference it but I'd rather it worked fine in the first place to prevent any complications in the future. Can anyone suggest why this might be happening and how I can tell PHP not to change my data into what it 'thinks' is correct? Many Thanks - Trev Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/ Share on other sites More sharing options...
btherl Posted April 10, 2007 Share Posted April 10, 2007 How are you accessing the data (both the ini files and the database)? With which functions? Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225641 Share on other sites More sharing options...
trvo Posted April 10, 2007 Author Share Posted April 10, 2007 To access the .ini data I am using this: INI file: [Options] field=Yes PHP Code: // Build an array of data from the ini file $ini_array = parse_ini_file($ini_filename); // Output the data for the field: 'field' echo $ini_array['field']; // Outputs a 1 The MySQL code is as follows (the content of field is 'Yes' but outputs a 1): $id=$_GET['id']; $query = "SELECT * FROM `requests` WHERE `id`='".$id."'"; $result=mysql_query($query) or die(mysql_error()); $field=mysql_result($result,0,"field"); Many Thanks - Trev Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225659 Share on other sites More sharing options...
trvo Posted April 10, 2007 Author Share Posted April 10, 2007 Please note, this only happens when the data is a Yes or No. Other data values output fine. Kind Regards - Trev Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225660 Share on other sites More sharing options...
wildteen88 Posted April 10, 2007 Share Posted April 10, 2007 Why don't you give the radio buttons a value of YES and NO in the HTML rather than getting PHP to set the value in the php.ini Do you agree? Yes: <input type="radio" name="confirm" value="Yes" /> <input type="radio" name="confirm" value="No" /> That way when when you access the data submitted from the form which ever radio button the user submitted it will return the string 'Yes' or 'No' for $_POST['confirm'] rather than true or false. Remember radio buttons can only submit 1 value from the same group of radio buttons. You must name every radio button that belongs to a group the same name. Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225663 Share on other sites More sharing options...
trvo Posted April 10, 2007 Author Share Posted April 10, 2007 That is currently how it works... User submits HTML form > PHP creates .ini file > More forms add more data to .ini file > .ini file reads back with 1 / 0 in the Yes / No fields when pulled back via php. Yes / No fields are always radios. However, when you actually open the newly created .ini it has the correct value of Yes / No. Trev Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225669 Share on other sites More sharing options...
mjlogan Posted April 10, 2007 Share Posted April 10, 2007 Note: There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, and false. Values null, no and false results in "", yes and true results in "1". Characters {}|&~![()" must not be used anywhere in the key and have a special meaning in the value. Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225670 Share on other sites More sharing options...
trvo Posted April 10, 2007 Author Share Posted April 10, 2007 Thanks a ton, guess I better find some cunning way to 're word' them. Thanks again for all the help guys! Trev Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225672 Share on other sites More sharing options...
btherl Posted April 10, 2007 Share Posted April 10, 2007 Regarding your mysql .. are you 100% sure that you are having "Yes" and "No" converted to bools? Did you check them before you put them into the database, while they are in the database and also when you took them out? The reason I am suspicious is that SQL interfaces, including php's mysql interface, always return strings. You can see this in the return type on this page: http://sg.php.net/manual/en/function.mysql-result.php The function cannot return a boolean. Additionally, Mysql does not even have a boolean data type. It uses integers. Can you try var_dump($field) immediately after the code you posted? Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225689 Share on other sites More sharing options...
trvo Posted April 10, 2007 Author Share Posted April 10, 2007 Sorry, that's my mistake, I'm quite deep into this, I was getting confused as to which pages pull data from where...Best get organised...I'm up to 200 9kb-ish pages so far and counting Thanks - Trev Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225692 Share on other sites More sharing options...
wildteen88 Posted April 10, 2007 Share Posted April 10, 2007 Thanks a ton, guess I better find some cunning way to 're word' them. Thanks again for all the help guys! Trev Can't you wrap quotes around yes and no instead? That way PHP will tread yes and no as a string and not as a boolean. Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225697 Share on other sites More sharing options...
trvo Posted April 10, 2007 Author Share Posted April 10, 2007 Perfect, yes, that works! Thanks again for the help, seems like a very nice community, I will stick around and help out where I can Kind Regards - Trev Quote Link to comment https://forums.phpfreaks.com/topic/46398-php-defaults-to-boolean-from-yes-no/#findComment-225706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.