Codeigniter select_sum generate wrong query - php

I worked on a Codeigniter 2 project, there I have an active record like:
$query = $this->db->select_sum('Cardit * GPA', 'sum')
->get('marks_info');
It generates query like:
SELECT SUM(`Cardit` * `GPA`) AS `sum` FROM `marks_info`
Recently I migrated my project to Codeigniter 3, but the same active record generated little different query like:
SELECT SUM(`Cardit *` `GPA`) AS `sum` FROM `marks_info`
that is wrong, it includes * with Cardit ('Cardit *') in SUM section.
Difference:
Can anybody tell me, how can I solve this issue in CodeIgniter 3 ?

Try this, It will give you your expected output
$query = $this->db->select('sum(Cardit * GPA) sum')->get('marks_info');

To avoid backticks within the query. You can assign false to protect_identifiers like :
$this->db->_protect_identifiers=false;
and run your query accordingly.

Related

SQL WHERE CONCAT LIKE NOT

I try to make SQL to search some string in database.
In this spesification, The SQL must be dont display one string in database.
my sql like this :
$query = "SELECT * FROM `chatuser` WHERE CONCAT( `fullname`,`image`) LIKE '%".$search_string."%' NOT (`$string is not be displayed`) " ;
is that possible ?
Thanks for help
The correct syntax of LIKE and NOT LIKE as two conditions would be:
SELECT * FROM chatuser
WHERE CONCAT(CustomerName,ContactName) LIKE '%t%'
AND CONCAT(CustomerName,ContactName) NOT LIKE '%m%';
You miss AND Between conditions. Also you have to repeat CONCAT(CustomerName,ContactName).
In the example above we are looking for all CustomerName+ContactName with a t in any place but if it doesn't have an m in any place.
From the docs found at https://www.w3resource.com/mysql/comparision-functions-and-operators/not-like.php
Example: MySQL NOT LIKE operator with (%) percent
The following MySQL statement excludes those rows from the table author, having the 1st character of aut_name ‘W’.
Code:
SELECT aut_name, country
FROM author
WHERE aut_name NOT LIKE 'W%';
And so it seems would work in your situation.

How to get result from mysql that contains in variable?

I'm not getting the result I need and I'm sure it is a small problem here.
I have a column(mfg_req) in my database which have a record with 10M.
My variable in php does have the text 10M40SABCDE.
What I want is to search in my table and get the results starting with this variable.
My MYSQL query does look like below but no results:
SELECT * FROM specific_req WHERE mfg_req LIKE '10M40SABCDE%'
I also tried the below query but no results
SELECT * FROM specific_req WHERE mfg_req LIKE '%10M40SABCDE%'
Also tried the below but it shows me all records except the one I need with 10M
SELECT * FROM specific_req WHERE '10M40SABCDE' LIKE CONCAT('%',mfg_req)
I have tried to put the % behind mfg_req but then it will show me all records including the one I need.
I cannot figure it out how to get the result I need. If someone can help me with my query I would appreciate it a lot.
Thanks!
Let's say:
$var = '10M40SABCDE';
...then your SQL statement must be:
$sql = "SELECT * FROM specific_req WHERE mfg_req LIKE '{$var}%'";
It would be best if you show a snippet of your code, too.

MYSQLi SELECT statement malformed or misordered

Using the following table "technotes":
And, using the following SQLi Query:
SELECT *
FROM
`technotes`
WHERE
`techgroup` LIKE '%dispatch%' OR
`techgroup` LIKE '%30243542%' AND
`expires` >= '2014-12-18' AND
`viewed` NOT LIKE '%30243542%'
What was expected was that alertid's: 23324325 & 23546576 (1st and last one) would be returned. But instead i am getting record 1,3,4,5 being returned. I am missing something in the order of operation or some other component in the SQLi select statement is malformed but not sure what. Can someone advise what I have left out or need to change so this works properly?
Try this:
SELECT * FROM
`technotes`
WHERE
(`techgroup` LIKE '%dispatch%' OR `techgroup` LIKE '%30243542%')
AND `expires` >= '2014-12-18'
AND (`viewed` NOT LIKE '%30243542%' OR ISNULL(`viewed`) )

Zend_Db_Adapter_Oracle: Select with ORDER BY, LIMIT & WHERE

I'm trying to select some records from an Oracle 11g Database. The Statement is used to implement some kind of "filter" function for an HTML Table.
Requirements: limit for paging and order the filtered results.
The query is created with Zend_Db_Select
*Works like a charm:*
$select->where('APPLICATIONS LIKE ?', '%MYAPP1%');
$select->where('APPLICATIONS NOT LIKE ?', '%GENESIS%');
$select->limit(20);
= 1 matching result (which is ok!)
The problem occurs when I try to order the filtered result:
$select->order('PATH ASC');
= 3 matching results ??
I think it has something to do with the query generated by Zend DB Select, it looks like this:
SELECT z2.*
FROM (
SELECT z1.*, ROWNUM AS "zend_db_rownum"
FROM (
SELECT "APPS".* FROM "APPS" WHERE (APPLICATIONS LIKE '%MYAPP1%') AND (APPLICATIONS NOT LIKE '%GENESIS%') ORDER BY "PATH" ASC
) z1
) z2
WHERE z2."zend_db_rownum" BETWEEN 1 AND 20
If I run the query without order everything is fine.
If I run the query without limit everything is fine.
If I run the query with order + limit -> wrong result.
If I take the statement and put the order after "BETWEEN 1 AND 20" it works like I want. But how to say Zend DB Select to change it?
Important: I'm doing the query against an Oracle VIEW, if I do it against a "table" it works too.
Obviously Oracle's interpretation of the query is not what the Zend framework intents:
Oracle seems to associate the ROWNUM with the row number count on the inner query before ordering, so the alias zend_db_rownum delivers wrong numbers in the where clause of the outer query.
Since we're not in control of the way the Zend framework generates the SQL in response to the limit() instruction, the only workaround I can think of is skipping the Zend limit() instruction, and instead doing something along the lines of:
$select->where('APPLICATIONS LIKE ?', '%MYAPP1%');
$select->where('APPLICATIONS NOT LIKE ?', '%GENESIS%');
$select->order('PATH ASC');
$sql = $select->__toString();
$sql = "select * from (" . $sql . ") where ROWNUM between 1 and 20";
$stmt = $db->query( $sql, array());
$result = $stmt->fetchAll();
Of course, this workaround is only legitimate in case you're not developing cross-DB, so your code doesn't have to be DB agnostic.
Meaning, you will restrict your solution to Oracle if you use my suggested workaround.
If you checked that there is no error in SQL generation, and really no different conditions in WHERE clause, it may be Oracle server bug. To check it, try it on different Oracle server version or different Oracle server patch level.

My Sql DATE_SUB() and codeigniter Active records issue?

i am using codeigniter active records to execute a simple mysql query to select data in last one hour.
so my query is:
SELECT * FROM tablename WHERE added_datetime >= DATE_SUB(NOW(),INTERVAL 1 HOUR)
and my codeigniter code to form this query is:
$data=array(
'added_datetime >='=>'DATE_SUB(NOW(),INTERVAL 1 HOUR)',
);
$query=$this->db->get_where('tablename',$data);
now the issue is codeigniter adds a single quotes around the DATE_SUB function and due to this the query not works on mysql server.
codeigniter produces:
SELECT * FROM (`tablename`) WHERE `added_datetime` >= 'DATE_SUB(NOW(),INTERVAL 1 HOUR)'
i also tried by adding FALSE as 3rd parameter in get_where but stil not worked
$query=$this->db->get_where('tablename',$data,FALSE);
it also produced the same query as above.
So please suggest me how to solve this issue.
-Thanks
Try doing something like this instead:
$this->db->where("added_datetime >= DATE_SUB(NOW(),INTERVAL 1 HOUR)", NULL, FALSE);
$query = $this->db->get('tablename');
$this->db->get_where(); doesn't provide the function to unescaping your where clause.

Categories