Codeigniter db->where_in adding '' - php

I am having a problem with where_in . I am trying to get the shop name which possess the lookbook had the specific point id
$this->db->select('shop');
$this->db->from('shopify_lookbook');
$this->db->where_in('lookbook_id', 'SELECT lookbook_id FROM shopify_point WHERE point_id = $pointid');
The problem is the query it generate
SELECT `shop` FROM `shopify_lookbook` WHERE `lookbook_id` IN('SELECT lookbook_id FROM shopify_point WHERE point_id = 543')
It will give blank but when I try in mysql without '' in IN() like below
SELECT `shop` FROM `shopify_lookbook` WHERE `lookbook_id` IN(SELECT lookbook_id FROM shopify_point WHERE point_id = 543)
It returns the shop name that I want. How can I erase '' in $this->db->where_in()

You might use where instead and to construct your IN clause there:
$this->db->where('lookbook_id IN (SELECT lookbook_id FROM shopify_point WHERE point_id = $pointid)', NULL, FALSE);

Related

Getting Empty array on INNER query in MySQL

I am a newbie in PHP/MySQL and this is my first question on stackoverflow, so please ignore any mistake if I have made any.
I am trying to get records from a table through this query.
$CId = $this->input->post('Child_id'); //child_id is the value from a textfield
$r_detail = ("SELECT *
FROM `result_details`
WHERE ResultId
IN (SELECT Result_Id
FROM `results`
WHERE childId = '".$CId." ') " );
this query is returning an empty $r_detail. However when I query the same statement in PHPMyAdmin in localhost and change "$CID" to a number, I get the desired records.
Any Help will be much appreciated.
You have a space in INNER JOIN WHERE: '".$CId." ' should be '".$CId."'.
$r_detail = ("SELECT *
FROM `result_details`
WHERE ResultId
IN (SELECT Result_Id
FROM `results`
WHERE childId = '".$CId." ') " );
has just string...you need to call some db function to get the data such as in code igniter may be
$this->db->query($r_detail);

Yii framework migrate

So, basically what i want is to make a file that will make some changes to my database. I have to enter some data, but to be faster, I want to use the sql IN operator. So what I want should look something like this:
$this->update('basicInfo', array('regionId' => 1),
'WHERE countyId IN (SELECT id FROM countyTable WHERE regionId = 1 )')
regionId should be set to 1 when the countyId is 3,6,7,9,4 and so on
I know this won't work but I don't know how to make it work and is it possible to make it work.
See CDbCommand where function doc for more information about how to use sql 'in' criteria.
I would do it like this:
// retrieve county ids for regionId = 1
$dbConn = $this->getDbConnection();
$countyTableIds = $dbConn->createCommand()
->select('id')
->from('countyTable')
->where('regionId = 1')
->queryAll();
// prepare condition as array.
$condition = array('in', 'countyId', $countyTableIds);
// update
$this->update('basicInfo', array('regionId' => 1), $condition);
Use the ANY clause
UPDATE mytable
SET status = 'inactive'
WHERE countyId = ANY (SELECT id FROM countyTable WHERE regionId = 1 )

using conditional statement in WHERE clause mysql

If keep_base_amount is null then I want to use country_id=236 in where clause, else I want to use keep_base_amount's value as in country_id.
I have query something like this:
SELECT * FROM account_treasury_local
WHERE
CASE WHEN keep_base_amount IS NOT NULL THEN country_id = keep_base_amount
ELSE country_id = '236' END
There is a record in database. But, I am getting nothing in result. Is there anything missing/wrong in above query.
As simple as this
SELECT * FROM account_treasury_local
WHERE (keep_base_amount IS NOT NULL AND country_id = keep_base_amount) OR (keep_base_amount IS NULL AND country_id = '236')
Have you tried:
SELECT * FROM account_treasury_local
WHERE
(keep_base_amount IS NOT NULL AND country_id = keep_base_amount)
OR
(keep_base_amount IS NULL AND country_id = '236')
I'm not sure I'm understanding your question correctly.

How to search data in combination of field

I have a student table with first_name and last_name. when i apply the search on first name or last name it give me the correct record but when i search with complete name it does not return any thing. Here is my query
SELECT
`student_personal_info`.`spi_id` AS `sid`,
`student_personal_info`.`spi_first_name` AS `fname`
FROM
`student_personal_info`
WHERE
((spi_first_name like '%test developer%') OR (spi_last_name like '%test developer%')
ORDER BY
`spi_first_name` ASC
Is there any way to make it searchable with both field combination.
It will give record in case we enter first name .middle name and last name but don't fetch in case of firstname and last name not middle name.
SELECT `student_personal_info`.`spi_id` AS `sid` , `student_personal_info`.`spi_first_name` AS `fname`
FROM `student_personal_info`
WHERE (
spi_scholar_no LIKE '%amir hussain khan%'
)
OR (
CONCAT( spi_first_name, ' ', spi_middle_name, ' ', spi_last_name ) LIKE '%amir hussain khan%'
I have also try this but its working for exact match only.
SELECT `student_parents_info`.`spri_spi_id` AS `sid`
FROM `student_parents_info`
WHERE (MATCH (spri_first_name, spri_middle_name, spri_last_name) AGAINST ('Pramod Kumar Sharma*' IN BOOLEAN MODE ) )
ORDER BY `spri_first_name` ASC
LIMIT 0 , 30
Please give any other suggestions.
Try searching using concat()..
SELECT `student_personal_info`.`spi_id` AS `sid` , `student_personal_info`.`spi_first_name` AS `fname`
FROM `student_personal_info`
WHERE CONCAT( spi_first_name, ' ', spi_middle_name, ' ', spi_last_name ) LIKE '%test%'
ORDER BY `spi_first_name` ASC
It always works for me .
Try like this
SELECT `student_personal_info`.`spi_id` AS `sid`, `student_personal_info`.`spi_first_name` AS `fname`
FROM `student_personal_info`
WHERE
((spi_first_name like '%amir khan%') OR (spi_last_name like '%test develope%'))
ORDER BY `spi_first_name` ASC
you are missing ')'
Split your test develope to test and develope using php, then write query like this :
$first = 'test';
$last = 'develope';
$query = "SELECT `student_personal_info`.`spi_id` AS `sid`, `student_personal_info`.`spi_first_name` AS `fname` FROM `student_personal_info` WHERE
(spi_first_name like '%".$first."%') OR (spi_last_name like '%".$last."%')
ORDER BY `spi_first_name` ASC";
If you have more than two words, Split the words and create the dynamic where query.
Try with CONCAT:
SELECT
`student_personal_info`.`spi_id` AS `sid`,
`student_personal_info`.`spi_first_name` AS `fname`
FROM
`student_personal_info`
WHERE
CONCAT(spi_first_name, ' ', spi_last_name) LIKE '%test develope%'
ORDER BY
`spi_first_name` ASC
Do you receive a full name to perform the search with? or do you always get 2 separate (first and last name)?
If you get one full name string, change your query to concatenate first and last name, and use
GROUP BY HAVING(full_name LIKE %search_term%)
if you get two separate strings, just change your OR to AND between the first and last name LIKE tests.

Calling a PHP Function into sql query

I have a MySQL table called "invoice_prod" :
id | qte | price | id_tva
And another table called "tva" :
id_tva | content
Now, I want to find the total of an invoice, so I made this query and it works.
$sql_mnt = mysql_query("
SELECT SUM( (qte * price) * ( 1 + (tva / 100))) AS total_un_prod
FROM invoice_prod
WHERE id='$id_invoice'
");
I have just one problem with this query. The "tva" return the "id of the tva" and not its content. Which I have to extract its content from the table "tva".
Clearly, this query return this value of the tva : "1", or, it should return "18%" which is the content of the tva's ID.
I tried to make a PHP function that extracts the tva's content from an id and include it into the SQL query like this :
$sql_mnt = mysql_query("
SELECT SUM(( qte * price) * ( 1 + (" .
tva_get_by_id(tva) . "/ 100))) AS total_un_prod
FROM invoice_prod
WHRE id='$id_invoice'
");
But it doesn't work.
What should I do ?
Thanks a lot for your help and have a nice day :)
use
$sql_mnt = mysql_query("
SELECT SUM((`qte`*`price`)*(1+(`content`/100))) AS `total_un_prod`
FROM `invoice_prod`
LEFT JOIN `tva` USING (`id_tva`)
WHERE `id`='".mysql_real_escape_string($id_invoice)."'");
this connects the two tables within mysql, so you can use the content field directly, which I presume holds the TVA percentage.
edit formatted and addressed the escaping of id_invoice as well (I guess it is int so a cast would suffice, but if not, this will work as well)
I am not sure I understand correctly what you're doing here, but it looks like what you want is an INNER JOIN on the tva table, and then use tva.content in your calculation. Is that correct?

Categories