I have a db that has a column that is empty and not using NULL.
How do I select the below column if its empty?
$this->db->where("{$this->_table['shop_shipping_rules']}.shop_shipping_rule_name");
Code:
$this->db->or_where("{$this->_table['shop_shipping_rules']}.shop_shipping_rule_country_iso", NULL);
$this->db->or_where("{$this->_table['shop_shipping_rules']}.shop_shipping_rule_country_iso", $country_iso);
$this->db->or_where("{$this->_table['shop_shipping_rules']}.shop_shipping_rule_region_code", $shop_shipping_rule_region_code);
$this->db->where("{$this->_table['shop_shipping_rules']}.shop_shipping_rule_name");
Try this
$this->db->where("{$this->_table['shop_shipping_rules']}.shop_shipping_rule_name",'');
works for me in Yii and Zend..
Related
I have a table with 5 columns and when I query select all columns, 1 column is ignored.
My table columns: id setting_key created_at updated_at app_setting
My query looks like this:
$result = DB::connection($token)->table('settings')->select('*')->get();
The missing column is app_setting and this exact same query is used in another part of my code and returns all 5 columns including app_setting.
If I add an addSelect to the query as so:
$query = DB::connection($token)->table('settings')->select('*');
$result = $query->addSelect('app_setting')->get();
I get an error 'Column not found: 1054' even though the column most definitely exists. Why would I be getting this error if the column exists and why does my original query not return all of the columns as it does in other areas of my code?
Edit: When I directly query the app_setting column, I get the values returned, but still get 'Column not found'. How is that possible?
first check your .env, that DB_database and DB_username and DB_password are correct or not. use the code below in the controller then run that function and post the result here
$result = DB::table('settings')->where('id',1)->first();
dd($result->app_setting);
before running the query fill a dummy data in the database.
I have the following which only inserts to column two if the value is empty:
UPDATE name
SET one=?,
two=COALESCE(two, ?),
three=?
WHERE id = ?
However, this only works if column two if the value is null. How can I get this to work if it is either NULL OR an empty string.
It should always insert to one and three. It should only insert to two if it is NULL or empty
I am using SQLite with PHP
If by "empty" you mean an empty string, you can use nullif():
two = COALESCE(NULLIF(two, ''), ?)
The more general solution is a CASE expression.
Don't store empty strings in TEXT columns.
Update the table to replace all empty strings with NULLs:
UPDATE name
SET two = NULL
WHERE TRIM(two) = ''
Now you can use your query, which is correct and in any other query you only need to check for NULL.
in my php app I need to update the datatime in mysql table where the column datatime is null or like '0000-00-00' and two foreign key have two specific values.
I try this code to update:
$query3="UPDATE prestiti SET DATA_FINE='".$datafine."' WHERE DATA_FINE='0000-00-00' AND ID_U=".$id_utente." AND ID_L=".$id_libro."";
$risultato4 = mysql_query($query3,$conn) or die ($query3);
The code doesn't work and when it works it change the column DATA_FINE in every record where the ID_U and ID_L correspond to the specific value also if DATA_FINE has already a value.
So what is the problem? I tried also to use DATA_FINE IS NULL into my WHERE but it doesn't work.
$datafine was setted like a datetime variable that I use to insert into another column and it works!
Thanks Matt the answer to my question is:
$query3="UPDATE prestiti SET DATA_FINE='".$datafine."' WHERE DATA_FINE LIKE '%0000-00-00%' AND ID_U=".$id_utente." AND ID_L=".$id_libro."";
$risultato4 = mysql_query($query3,$conn) or die ($query3);
Where I edit DATA_FINE='0000-00-00' into DATA_FINE LIKE '%0000-00-00%'
I´d like to add an extra column to the select for formatting purposes. The problem is that when I do
$this->db->select("NULL as ExtraColumn1")
codeigniter treats NULL as a column, so when it generates the query it's something like
SELECT `NULL` AS ExtraColumn1 ...
which of course returns a DB error. The same happens when I try
$this->db->select(" '' as ExtraColumn1")
Is there any way of doing it using activerecord?
Thanks
Tell CodeIgniter not to wrap fields in ticks. You do this by passing false as the second parameter in select():
$this->db->select("NULL as ExtraColumn1", false);
From the manual:
$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.
how doHow do we do
select table.value as table_value from table in codeigniter?
The AS part doesnt work because when i try to access the value,
this doesnt work:
$qry_inp = 'select table.value as table_value from table '
$query = $this->db->query($qry_inp);
echo $query->row('table_value ');// this will be empty, but it shouldn`t be
doesn`t matter if its in AR or simple query
Pretty simple thing.
$this->db->select('COLUMN_ACTUAL_NAME as `COLUMN_NAME_YOU_WANT_TO_SHOW`');
i'm joining two tables in which column names are same so i separate both tables columns by using as keyword , this is how you can use AS in codeigniter
$this->db->select("departments.name AS 'dname'");
$this->db->select('positions.name');
Where is that behaviour documented? row doesn't take a column name as a parameter; it optionally takes a row number, and that's it. Access it like any other field:
echo $query->row()->table_value;