I just started learning php and the best way to learn is to work on examples..going through this code I still can't understand what it happening here after looking and relooking in my text books..can someone explain this code to me please?
$_TABLES['the_list'] =
"`t_id` varchar(".T_ID_MAX_CHARS.") NOT NULL default '' UNIQUE, ".
"`tnet` varchar(".TNET_MAX_CHARS.") NOT NULL default '".DEFAULT_TNET."', ".
"`t_version` int unsigned NOT NULL default '0', ".
"`flag_new` bool NOT NULL default '1', ".
"`flag_used` bool NOT NULL default '0', ".
"`comment` tinytext NOT NULL";
what does the syntax "$variable[something]" do?
According to what I see,
there is a variable called _TABLES which is an array
It is defining a key called the_list within this, which contains the text to generate a table in a MySQL database
MORE INFO: http://php.net/manual/en/language.types.array.php
An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.
array(
key => value,
key2 => value2,
key3 => value3,
... )
PD: You have to be more specific because you're asking something without context.
I am using Cakephp 3 and trying to get some rows/records from a table having a field as non empty.
For example:-
I have a areas table with a field named Area_block. I need to fetch records for those rows which have something inside Area_block. In simple words, I don't need empty or NULL Area_block rows.
Area_block is not a NULL field by default. So it remains empty/blank.
I have tried
$conditions = ['Area_Block IS NOT NULL'];
$conditions = ['NOT' => array('Area_Block' => '')];
$conditions = ['Area_Block <>' => ''];
$conditions = ['Area_Block !=' => ''];
but nothing works !!
My environment includes MSSQL server 2008 and PHP 5.6 on apache.
Please help !!
I understand your table holds records with Area_Block set both to NULL and '' (empty string).
Try the following:
$conditions = [['Area_Block IS NOT' => null], ['Area_Block IS NOT' => '']];
This should also work:
$conditions = ['Area_Block IS NOT' => [null,'']]; //haven't tested it
However, in order to reduce complexity, in the database you may want to set the Area_Block field either to default to NULL, or default to '' and prevent the field from accepting NULL values.
Working exclusively with NOT NULL values might be easier. If you alter the table to reflect this, you would only need to check for '' values:
$conditions = ['Area_Block IS NOT' => ''];
See Automatic IS NOT NULL Creation in the 3.x Cookbook.
I think the solution provided by #InigoFlores worked to some extend but was failing for some records.
Here's what I did to make it work perfectly.
$conditions = ['DATALENGTH(Area_Block) >' => 0];
I Hope it helps someone.
In CodeIgniter, I want to prep a value returned from a form so that if it is a 0, it will actually be inserted as NULL.
I created a function outside of my controller class:
function prep_zero_to_null($int)
{
if ($int == 0)
{
return NULL;
}
else
{
return $int;
}
}
And at the form validation, I do:
$this->form_validation->set_rules('category_id', 'Category',
'required|integer|prep_zero_to_null');
However, CI still tries to insert zeroes as '0' in the database, which breaks one of my foreign key constraints.
Interestingly enough, if I replace NULL by, say, 25 in the prep_zero_to_null function, CI will indeed recognize it and insert 25 instead of '0'. So my prepping function is indeed getting called, but CI won't allow NULL as a result of it and instead converts it to '0'.
How can I achieve what I want?
Edit: For those wondering, the category_id field does allow null:
`category_id` int(10) unsigned DEFAULT NULL
And the exact error is:
INSERT INTO `articles` (`category_id`, `order`, `title`, `text`)
VALUES ('0', '0', 'test', 'test')
^
Should be NULL
Cannot add or update a child row: a foreign key constraint fails
(`db`.`articles`, CONSTRAINT `articles_ibfk_1` FOREIGN KEY
(`category_id`) REFERENCES `categories` (`id`)
ON DELETE SET NULL ON UPDATE CASCADE)
Just looking at this quickly I think the problem is your $int == 0. Is $int an actual 0 type integer or is it a string? In which case the proper check would be $int == '0'.
Codeigniter validation functions doesn't set the field value based on what you return, your validation function should either return TRUE or FALSE to state that something is valid or not.
If you're after changing the value of something, you'll need to accept variable by reference and modify it in the function then you can return TRUE so it passes the validation.
The best solution would be to make the check before inserting the data into database & not relying on the validation library to do this kind of dirty work.
If you want to insert null to the database you need to return a string with the value "null".
function prep_zero_to_null($int) {
return ($int == 0) ? 'NULL' : $int;
}
Have you tried just unsetting the variable. It's a bit ugly, but it should return NULL for the value then.
Tested here.
I submitted an issue at the GitHub bug tracker for CodeIgniter, as this appears to be a bug.
https://github.com/EllisLab/CodeIgniter/issues/2563
Right now, the workaround at model level is the following:
$category_id = prep_zero_to_null($this->input->post('category_id'));
$data = array
(
'category_id' => $category_id,
'order' => $this->input->post('order'),
'title' => $this->input->post('title'),
'text' => $this->input->post('text')
);
Edit: Apparently, this is the right approach, as there should only be strings at validation/controller level.
I have an int field in database and :disabled is supposed to be true false, I am assuming database gets boolean values as integer 0 and 1, but I am unsure.
function loadbyinput($name,$password,$ipnumber="0.0.0.0",$type="member",$disabled=FALSE){
$dbh = new PDO(...);
$statement=$dbh->prepare("insert into
actor(name,password,ipnumber,type,disabled)
values(:name,:password,:ipnumber,:type,:disabled)");
$statement->bindParam(":disabled", $disabled);
}
I am not writing any GUI at the moment so it is hard to test such things for me.
Depends on your schema. For boolean columns in the database you can use the following construct (there is a BOOLEAN construct, but it's just an alias for TINYINT):
`disabled` tinyint(1) NOT NULL DEFAULT '0'
Then when you bind, you can enforce a bool value:
$stmt->bindValue(':disabled', $disabled, PDO::PARAM_BOOL);
The equivalents get passed:
True = 1
False = 0
Everytime I try to make an insert statement into my dastabase I get a "Incorrect decimal value: 'NULL' for column 'bounty3' at row 1" error. How do I insert a null value into a decimal datatype? Should I just make the default value 0.00?
Incorrect decimal value: '' for column 'bounty3' at row 1 Whole query: INSERT INTO songs (userid, wavURL, mp3URL, genre, songTitle, BPM, insWanted, bounty, insWanted2, bounty2, insWanted3, bounty3, insWanted4, bounty4, insWanted5, bounty5, insWanted6, bounty6, insWanted7, bounty7, insWanted8, bounty8, insWanted9, bounty9, insWanted10, bounty10) VALUES ('12534545', '/audio/wav/jqmrgpfcMichael/135259578210secreason.wav', '/audio/mp3/jqmrgpfcMichael/135259578210secreason.mp3', 'Rock/Funk', 'titlee', '120', 'bass', '20.00', 'guitar', '20.00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '')
I tried this statement with a NULL value too. Here is the error:
Incorrect decimal value: 'NULL' for column 'bounty3' at row 1 Whole query: INSERT INTO songs (userid, wavURL, mp3URL, genre, songTitle, BPM, insWanted, bounty, insWanted2, bounty2, insWanted3, bounty3, insWanted4, bounty4, insWanted5, bounty5, insWanted6, bounty6, insWanted7, bounty7, insWanted8, bounty8, insWanted9, bounty9, insWanted10, bounty10) VALUES ('12534545', '/audio/wav/jqmrgpfcMichael/143922765110secreason.wav', '/audio/mp3/jqmrgpfcMichael/143922765110secreason.mp3', 'Rock/Funk', 'title', '110', 'bass', '110.00', 'guitar', '20.00', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL')
Oh.. you are trying to insert an empty string '' into bounty3. Replace it with NULL. I have also noticed empty strings for other possible numeric values eg bonty4. You should replace all empty strings with NULL for numeric values.
eg: mysql_query("INSERT INTO empty_number,number VALUES(NULL,1)");
EDIT: HEY HEY get the point, you cannot insert NULL to a numeric value as 'NULL' because this is a string, you should insert as NULL without any quotation marks
use blank, not null, if you want the default value.
I just dealt with this issue in MySql 5.1.61.
If the server is in strict mode, you must enter 0 instead of '' for decimal fields. Disabling strict mode allows you to just do '' to populate a null value.
Thanks, it worked using NULL when it comes empty:
$conn->bindParam(':'.$valor, ( $key[0] ? $key[0] : NULL ), PDO::PARAM_STR);
You can run this query to allow NULL values in 'bounty3' field.
ALTER TABLE songs CHANGE bounty3 bounty3 DECIMAL(10,0) NULL;
Make sure the field type allows for NULL values and that the default is NULL.
Then use:
UPDATE table_name SET date_field=IF('$date_value'='',NULL,'$date_value')
This works for inserts or updates.
I have tried using $date_value = NULL and I have even tried unset($date_value) before inserting or updating, and it never worked on my decimal fields. MySQL always converted them to 0.00. The method above was the only solution that worked for me.
Make sure that the field is "nullable" (does not have NOT NULL in its definition)
Make field's default value NULL.
Ex.: price decimal(12,2) DEFAULT NULL
Now, to test it, store some number, then '' (an empty string) into this field — it should become NULL in the end.
The field is most likely numeric. If the field is numeric then only numbers are accepted. Empty string is not accepted
insert into songs (...,bounty3) values(...,'')
neither the NULL string
insert into songs (...,bounty3) values(...,'NULL')
('NULL' which is just a string, nothing to do with the actual NULL value, so 'NULL' si similar to 'HONEY BEE' if you want).
So if the column is numeric then it will only take numbers. However if the column is NULLABLE then it will accept NULL, too. That means your insert must have
insert into songs (...,bounty3) values (..., NULL)
If you push NULL on such a column and the column has a default value then DEFAULT value will be used instead of the NULL you are pushing.
You need to make sure that your database schema allows NULL values for that column. Run describe <tablename>; in your MySQL client to see what the schema for that table is.
If the schema does not allow null then you can use alter table to change the schema. Make sure you don't specify NOT NULL for that column and you should be fine.
Another option is to make sure that in your source data, the empty cells or fields contain 'NULL' before you import them in MySQL. This way, MySQL will recognize these fields as being really NULL, and won't transform them to 0's.
It worked por me when POST data is received:
...
if ($value == '') {$value = '0.0';}
...