Codeigniter subquery exists - php

Im trying to do a subquery with
$this->db->where(" EXISTS (SELECT * FROM myTable)");
But it doesnt work, the output of this is: myquery + WHERE 'EXISTS (SELECT * FROM myTable);
That quote before the EXISTS makes the query unresolvable!
Does anyone knows how to solve it?
Thanks!

please remove space before and after EXISTS keyword.that does not display any error.
$this->db->where("EXISTS(SELECT * FROM myTable)");

Maybe you could try to set the escape to false by using
$this->db->where(" EXISTS (SELECT * FROM myTable)", null, false);
This is the snippet of where() in DB_active_rec.php
public function where($key, $value = NULL, $escape = TRUE)

Just try this.
Instead of using 'where' clause, please write down the complete query string & execute the query using $this->db->query();
$qry_string= $yourquery . "WHERE EXISTS (SELECT * FROM myTable)";
$this->db->query($qry_string);

Related

MySQL query returns no result

I have fields id and date defined as VARCHAR(16)
When I do this query:
SELECT *
FROM `members`
WHERE `id` = '4412040999876'
AND `date` = '201706054783'
I get no results.
When I do it like this:
SELECT *
FROM `members`
WHERE `id` = 4412040999876
AND `date` = 201706054783
Note - without the quotes - I get the result I am expecting.
EDIT: Here is the code used - I am not manually adding quotes. CI's DB class is adding them.
public function get_member_match($id, $mem, $field = 'name')
{
$sql = "
SELECT *
FROM `members`
WHERE `id` = ?
AND `" . $field . "` = ?
";
$sql_array = array($id, $mem);
$q = $this->db->query($sql, $sql_array);
return $q->result_array();
}
And I call this function as:
$this->members_model->get_member_match($id, $date, 'date');
I output the query, and the variables are matched correctly, no errors, only the quotes.
Any idea why? I never had this problem before. Working on CodeIgniter 3 using Query Builder.
EDIT2: Summary of findings so far:
Localhost (MySQL 5.6.24) works, server (MySQL 5.5.55-0+deb7u1) doesn't.
The problem occurs in my code and in PHPMyAdmin on the server but works locally, so I eliminate a code issue.
The show variables like 'char%' query shows all character set settings identical on local and on the server.
Database and fields have the same encoding on both server and local.
Does not seem to be a casting issue as many of the comments suggest, as the problem is not present on localhost, only on the server, unless the server has config or other issues.
...?
id might be defined as integer in your database. To match against integer fields you do not need to use quotes. Quotes are used when you match against string or text fields.
This should cast it back from the codeigniter's auto cast:
SELECT *
FROM `members`
WHERE `id` = '4412040999876'
AND `date` = '201706054783'
SELECT *
FROM `members`
WHERE CAST(`id` as INTEGER) = '4412040999876'
AND `date` = '201706054783'
Try to use this
$this->db->from('members');
$this->db->where('id',$id);
$this->db->where($field,$mem);
$q = $this->db->get();
return $q->result_array();
It's because the quotes imply that it is a string, whereas id field is an integer and must be written without quotes.
Same for date. Internally date is stored as an integer but is able to convert string into dates as long as they have an appropriate format

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);

Select From table results that don't equal to $var?

How do I make it pick all results that are not equal to the $var , here's my code.
$opti=mysql_query("SELECT * FROM table1 WHERE imageid=$image_id");
while ($vari = mysql_fetch_array($opti)) {
$var = $vari['tagid'];
$options=mysql_query("SELECT * FROM table WHERE id!=$var");
while ($taghe1 = mysql_fetch_array($options)) {
$tagname = $taghe1['name'];
echo "".$tagname.", ";
} }
Try:
$options=mysql_query("SELECT * FROM table WHERE id<>{$var}");
You can probably see from the answer you accepted that adding the quotes solved your problem. Another way to do this is to just use one query. I will show an example using mysqli instead of the deprecated mysql, but the same query should work in mysql if you must use it. I added a couple of other suggestions that aren't really addressing your question, but make me feel better about my answer.
// Please be sure to escape $image_id before using it like this
$unused_tags = mysqli_query($db, "SELECT `name` FROM `table` AS t
LEFT JOIN (SELECT tagid FROM table1 WHERE imageid=$image_id) AS t1
ON t.id = t1.tagid WHERE t1.tagid IS NULL;");
while ($tag = mysqli_fetch_array($unused_tags)) {
$tags[] = htmlspecialchars($tag['name']); // escape your output
}
echo implode(", ", $tags); // doing it this way eliminates the trailing comma
You could use this:
$options=mysql_query("SELECT * FROM table WHERE id not in ('$var')");
You could have multiple values here, e.g.
$options=mysql_query("SELECT * FROM table WHERE id not in ('$var1', '$var2', '$var3')");

Mysql Order by issue in WHERE IN function

SELECT * FROM (`poll`) WHERE `poll_id` IN ('6,10,5,9,1') AND `poll_status` = '1'
I tried this query. I expected It would give result as I gave. But It sorting the result automatically in ascending. I need result as I give. Kindly give me a solution. Thanks in advance.
You can use FIELD in mysql:
SELECT *
FROM (`poll`)
WHERE `poll_id` IN (6,10,5,9,1)
AND `poll_status` = 1
ORDER BY FIELD(`poll_id`,6,10,5,9,1)
You have to separate your ids like that:
SELECT * FROM (`poll`) WHERE `poll_id` IN (6,10,5,9,1) AND `poll_status` = '1'

codeigniter, active record, having problem

I have a problem with Code Igniter having clause.
I need to produce the following SQL with active record:
SELECT *
FROM A
GROUP BY A.X
HAVING A.Y = 'test'
But using the following code:
$this->db->select('*');
$this->db->from('A');
$this->db->group_by('A.X');
$this->db->having('A.Y','frontend');
Produces:
SELECT *
FROM A
GROUP BY A.X
HAVING A.Y = test
And it seems impossible to escape the string value... Or is it?
Write the having clause in a such clumsy way:
$this->db->having('A.Y = "frontend"', null, false);

Categories