Exclude the values in "IN" clause - php

How should I exclude the values in "IN" clause?
$Graduates = "45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,73,74,75,76,84,85,86,87,88,89,91,
92,93,94,95,96,98,99,100,101,103,104,105,106,107,108,109,110,111,112,113,114,117,122,123,126,127,131,135,136,137,
138,139,140,142,143,146,156"; //MajorID
$QryFirstNursing = "SELECT *
FROM ".$SemYearSettings->getYearSem($Enroll)."
WHERE ".$SemYearSettings->getYearSem($Enroll).".MajorID IN (".$Graduates.")
AND ".$SemYearSettings->getYearSem($Enroll).".Category = ".$CategoryIDN;
$resultFirstNursing = openQry($QryFirstNursing);
Please help guys. Thanks

#Ardit Meti rights. It look like this:
$Graduates = "45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,73,74,75,76,84,85,86,87,88,89,91,92,93,94,95,96,98,99,100,101,103,104,105,106,107,108,109,110,111,112,113,114,117,122,123,126,127,131,135,136,137,138,139,140,142,143,146,156"; //MajorID
$tableName = $SemYearSettings->getYearSem($Enroll);
$QryFirstNursing = "SELECT *
FROM {$tableName}
WHERE {$tableName}.MajorID NOT IN ('{$Graduates}')
AND {$tableName}.Category = {$CategoryIDN}";
$resultFirstNursing = openQry($QryFirstNursing);

Related

How to find month wise data from mysql in CI

Just a small quick question,
I want to form the query like this
SELECT *
FROM users
WHERE MONTH( `birthday` ) = MONTH( '1999/05/19' )
Now while in my PHP function, I wants to perform same but I am getting this from post variable , after forming the query my output is like this:
SELECT *
FROM `users`
WHERE MONTH(birthday) = 'MONTH(\"1999-05-19\")'
I want to remove \ from code.
My Code for the following is :
$birthday = $this->input->post('birthday');
$where['MONTH(birthday)'] = 'MONTH('.'"'.$birthday.'"'.')';
$result = $this->User_model->getAnyData($where);
Can anyone tell me where I am going wrong?
use like given below may help you
$birthday = $this->input->post('birthday');
$where['MONTH(birthday)'] = "MONTH(".$birthday.")";
$result = $this->User_model->getAnyData($where);
If the POSTed data contains backslashes, use PHP's stripslashes() to remove them: http://php.net/manual/en/function.stripslashes.php.
Depending whether your string was enclosed by single or double quotes will depend on what you do:
<?php
$sql = 'SELECT *
FROM `users`
WHERE MONTH(birthday) = MONTH("1999-05-19")';
Should work. Also:
<?php
$sql = "SELECT *
FROM `users`
WHERE MONTH(birthday) = MONTH('1999-05-19')";
To remove the single quote, you need to pass false paramete in active record.
$this->db->where('MONTH(birthday)', 'MONTH(YOUR_DATA)', FALSE);
So the query would be like:
$this->db->select('*');
$this->db->from('TABLE_NAME');
$this->db->where('MONTH(birthday)', 'MONTH(YOUR_DATA)', FALSE);
Try with this,Fetching only month.
$birthday = $this->input->post('birthday');
$birthday = DateTime::createFromFormat('d/m/Y', $birthday)->format('m');
$where['MONTH(birthday)'] = $birthday;
$result = $this->User_model->getAnyData($where);
SELECT * FROM users WHERE MONTH( `birthday` ) = '05 ';
DISPLAY MONTH WISE DATA WITH PHP CODEIGNITER
$query = $this->db->query("SELECT COUNT(id) as count,MONTHNAME(created_at) as month_name FROM users WHERE YEAR(created_at) = '" . date('Y') . "'
GROUP BY YEAR(created_at),MONTH(created_at)");
$record = $query->result();
$output = [];
foreach($record as $row) {
$output[] = array(
'month_name' => $row->month_name,
'count' => floatval($row->count)
);
}
$data['output'] = ($output);

MySQL query working in PHPMyAdmin but not working in PHP

I have written a MySQL query:
$feedItem->tags =
$result = mysqli_query("SELECT *
FROM qzxh_k2_tags, qzxh_k2_tags_xref
WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
AND qzxh_k2_tags.id = '406'
AND qzxh_k2_tags_xref.itemID = '".$item->id"'");
while($tag = mysqli_fetch_array($result))
echo $tag;
The query itself shows the results I need in PHPMyAdmin, but for some reason this isn't showing anything when used in PHP. Any ideas where I'm going wrong please?
You are missing concatenation operator (dot)
Change
"SELECT *
FROM qzxh_k2_tags, qzxh_k2_tags_xref
WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
AND qzxh_k2_tags.id = '406'
AND qzxh_k2_tags_xref.itemID = '".$item->id"'"
// You are missing dot . here ^
To
"SELECT *
FROM qzxh_k2_tags, qzxh_k2_tags_xref
WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
AND qzxh_k2_tags.id = '406'
AND qzxh_k2_tags_xref.itemID = '".$item->id."'"
Use proper string concatenation in your sql query. Find below corrected query.
$result = mysqli_query("SELECT *
FROM qzxh_k2_tags, qzxh_k2_tags_xref
WHERE qzxh_k2_tags.id = qzxh_k2_tags_xref.tagID
AND qzxh_k2_tags.id = '406'
AND qzxh_k2_tags_xref.itemID = '".$item->id."'");

PHP: Variable into string query

I'm trying to do a query based into a value obtained in a previous query. Something like that:
$variableid = 100;
$query_prev = "SELECT query FROM queries_table WHERE id = 1";
$result_prev = pg_query($pg,$query_prev);
$row_prev = pg_fetch_array($result_prev);
$final_query = $row_prev['query'];
$row_prev['query'] value would be "SELECT * FROM other_table WHERE id = $variableid";
$final_query value at this point is: "SELECT * FROM other_table WHERE id = $variableid"
/* but that I want is this value: */ "SELECT * FROM other_table WHERE id = 100"
Use if statement before you do the next select so:
<?php if ($row_prev['query']= 100){
SELECT .......... etc.
}else
SELECT from other
?>
/Hope that's what you wanted
Solved:
$variableid = 100;
$query_prev = "SELECT query FROM queries_table WHERE id = 1";
$result_prev = pg_query($pg,$query_prev);
$row_prev = pg_fetch_array($result_prev);
$query = $row_prev['query'];
eval("\$final_query = \"$query\";");

Short Code for PHP string with variable

I have a query string that contains a variable like this
$field_name = 'features';
$value = '5';
$query = "SELECT * FROM Table WHERE $field_name\_tid = '$value'";
My goal is to print out the $query like this SELECT * FROM Table WHERE features_tid = '5';
I put \_ there hoping it would work as escape character, but it didn't work. Is there any way to achieve this without use methods like ". $field_name ." and modifying original variable value?
yes:
$query = "SELECT * FROM Table WHERE {$field_name}_tid = '$value'";
You can use:
$query = "SELECT * FROM Table WHERE {$field_name}_tid = '$value'";

How to select with a binary field ? (php,mysql)

Try to select use "where" clause in a mysql statement:
e.g.
Table: X with a ID column which is BINARY data type. Then save in a variable in php
$aid = $row["id"];
How do i use this variable later when I try to select from table
$where = "where `ID` = '$aid'";
$query = "SELECT * FROM X ".$where;
Return 0 row.
Does anyone know why?
Answering my own question.
Just figured out:
$where = "where HEX(ID) = 'bin2hex($aid)'";
$query = "SELECT * FROM X ".$where;
Does anyone know better solution?
Try below :
add BINARY in where clause.
$where = "where BINARY ID = '$aid'";
$query = "SELECT * FROM X ".$where;

Categories