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);
Related
$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}')";
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."'");
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'";
I'm wondering how to query a database using an array, like so:
$query = mysql_query("SELECT * FROM status_updates WHERE member_id = '$friends['member_id']'");
$friends is an array which contains the member's ID. I am trying to query the database and show all results where member_id is equal to one of the member's ID in the $friends array.
Is there a way to do something like WHERE = $friends[member_id] or would I have to convert the array into a string and build the query like so:
$query = "";
foreach($friends as $friend){
$query .= 'OR member_id = '.$friend[id.' ';
}
$query = mysql_query("SELECT * FROM status_updates WHERE member_id = '1' $query");
Any help would be greatly appreciated, thanks!
You want IN.
SELECT * FROM status_updates WHERE member_id IN ('1', '2', '3');
So the code changes to:
$query = mysql_query("SELECT * FROM status_updates WHERE member_id IN ('" . implode("','", $friends) . "')");
Depending on where the data in the friends array comes from you many want to pass each value through mysql_real_escape_string() to make sure there are no SQL injections.
Use the SQL IN operator like so:
// Prepare comma separated list of ids (you could use implode for a simpler array)
$instr = '';
foreach($friends as $friend){
$instr .= $friend['member_id'].',';
}
$instr = rtrim($instr, ','); // remove trailing comma
// Use the comma separated list in the query using the IN () operator
$query = mysql_query("SELECT * FROM status_updates WHERE member_id IN ($instr)");
$query = "SELECT * FROM status_updates WHERE ";
for($i = 0 ; $i < sizeof($friends); $i++){
$query .= "member_id = '".$friends[$i]."' OR ";
}
substr($query, -3);
$result = mysql_query($query);
I'm using the following code to sort MySQL queries into time/date:
mysql_select_db("user_live_now", $con);
$result = mysql_query("SELECT * FROM users_newest_post ORDER BY users_date_post DESC");
while($row = mysql_fetch_array($result))
{
print($row['user']);
}
instead of having the PHP run through and show all the values in the table can I have it show the values from an array?
So, you want to find specific users in the SQL query to return? Build your query programmatically:
$users = array('User1','John','Pete Allport','etc');
$sql = "SELECT * FROM `users_newest_post` WHERE ";
$i = 1;
foreach($users as $user)
{
$sql .= "`username` = '$user'";
if($i != count($users))
{
$sql .= " OR ";
}
$i++;
}
$sql .= " ORDER BY `users_date_post` DESC";
$result = mysql_query($sql);
Which would get you a query like:
SELECT * FROM `users_newest_post`
WHERE `username` = 'User1'
OR `username` = 'John'
OR `username` = 'Pete Allport'
OR `username` = 'etc'
ORDER BY `users_date_post`
DESC
So, you want to find all posts for a certain date or between two dates, kinda hard to do it without knowing the table structure, but you'd do it with something like this:
//Here's how to find all posts for a single date for all users
$date = date('Y-m-d',$timestamp);
//You'd pull the timestamp/date in from a form on another page or where ever
//Like a calendar with links on the days which have posts and pass the day
//selected through $_GET like page.php?date=1302115769
//timestamps are in UNIX timestamp format, such as you'd get from time() or strtotime()
//Note that, without a timestamp parameter passed to date() it uses the current time() instead
$sql = "SELECT * FROM `posts` WHERE `users_date_post` = '$date'"
$results = mysql_query($sql);
while($row = mysql_fetch_assoc($results))
{
echo $row['post_name'] . $row['users_date_post']; //output something from the posts
}
//Here's how to find all posts for a range of dates
$startdate = date('Y-m-d',$starttimestamp);
$enddate = date('Y-m-d',$endtimestamp);
//Yet again, date ranges need to be pulled in from somewhere, like $_GET or a POSTed form.
//Can also just pull in a formatted date rather than a timestamp and use it straight up instead, rather than going through date()
$sql = "SELECT * FROM `posts` WHERE `users_date_post` BETWEEN '$startdate' AND '$enddate'";
//could also do:
//"SELECT * FROM `posts` WHERE `users_date_post` > '$startdate' AND `users_date_post` < '$endate'"
$results = mysql_query($sql);
while($row = mysql_fetch_assoc($results))
{
//output data
}
To find posts for a specific user you would modify the statement to be something like:
$userid = 5; //Pulled in from form or $_GET or whatever
"SELECT * FROM `posts` WHERE `users_date_post` > '$startdate' AND `users_date_post` < '$enddate' AND `userid` = $userid"
To dump the result into an array do the following:
mysql_select_db("user_live_now", $con);
$result = mysql_query("SELECT * FROM users_newest_post ORDER BY users_date_post DESC");
while($row=mysql_fetch_assoc($result))
{
$newarray[]=$row
}
What you probably want to do is this:
$users = array("Pete", "Jon", "Steffi");
$users = array_map("mysql_real_escape_string", $users);
$users = implode(",", $users);
..("SELECT * FROM users_newest_post WHERE FIND_IN_SET(user, '$users')");
The FIND_IN_SET function is a but inefficient for this purpose. But you could transition to an IN clause with a bit more typing if there's a real need.
$sql = 'SELECT * FROM `users_newest_post` WHERE username IN (' . implode(',', $users) . ')';