$sql = "SELECT * FROM one_chat WHERE chat_email = '{$get_mail}' && chat_email = '{$session_email}' ";
Query is CORRECT, but not displaying result on AND condition, if use OR operator then both conditions becomes true and displays both data! Why so disguisting!!!
i think use this and check if it gives any result
$sql = "SELECT * FROM one_chat WHERE chat_email = '{$get_mail}' AND chat_email = '{$session_email}' ";
basically try to replace && with AND
according to the comments , this var value ( $get_mail ) is null , and you try to select data with condition this ( chat_email = '{$get_mail}' AND chat_email = '{$session_email}') ,
you need chat_email to be null and another value come from session .
you can change condition to "or"
or , if the both variables have the same value in your business, you can do that :-
$email = !empty($get_mail) ? $get_mail : $session_email;
$sql = "SELECT * FROM one_chat WHERE chat_email = '{$email}'";
You can also change your query like this
$sql = "SELECT * FROM one_chat WHERE chat_email = COALESCE('{$get_mail}','{$session_email}')";
Related
I have an associative array like
$where = array('name'=>'name','comp'=>'companyname')
This is my query
select * from tablename where = $where;
And i want to make a mysqli query generate
select * from tablename where name = 'name' and comp = 'companyname';
echo "select * from tablename where ".implode(' AND ' , preg_replace('/^(.*)$/e', ' "$1=\'". $where["$1"]."\'" ',array_flip($where)));
Try this. I set the conditions in a string and appended the string onto the end of the query. If you set the WHERE to be 1=1 +'yourstuff', you can build the query dynamically.
$queryappnd = '';
foreach($where as $i){
$column = array_key($i);
$value = $i;
$queryappnd .='AND'.$column.'='.$value;
}
$query = "SELECT *
FROM tablename
WHERE 1=1".$queryappnd;
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);
$qurym="SELECT * FROM referal_member WHERE `ref_cusid` = '$id' && `confirm` = '1'";
$resm=mysqli_query($con,$qurym);
$rowm=mysqli_fetch_array($resm);
Just try like this: [ if fields ( ref_cusid and confirm) are of INT type, then pass values $id and 1 without single quotes ( ' ), otherwise make change as below ]
$qurym = "SELECT * FROM referal_member
WHERE `ref_cusid` = ' " . $id . "'
AND `confirm` = '1'";
$resm = mysqli_query($con,$qurym);
// check query returns something or not
if(mysqli_num_rows($resm)){
// fetch data
$rowm=mysqli_fetch_array($resm);
}
else{
// no data found
}
Please remove quotes from confirm if it is integer datatype.
$qurym="SELECT * FROM referal_member WHERE `ref_cusid` = '$id' && `confirm` = 1";
$resm=mysqli_query($con,$qurym);
$rowm=mysqli_fetch_array($resm);
print_r($rowm); // to check results.
Or If ref_cusid is also integer type.
$qurym="SELECT * FROM referal_member WHERE `ref_cusid` = $id && `confirm` = 1";
$resm=mysqli_query($con,$qurym);
$rowm=mysqli_fetch_array($resm);
print_r($rowm); // to check results.
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'";
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;