-2

Database Structureenter image description hereI am trying raw query in an controller(Codeigniter) and i am trying to get the values from the range provided by the user on newDate and newDate2..My problem is it displays nothing..How do i write the query for this..I just want to display the qty,productname,bundle according to the voucherno(vno)...Here is my tried code

Controller Code:

$startdate         = $this->input->post('SDate');
$enddate           = $this->input->post('EDate');
$date              = str_replace('/', '-', $startdate);
$newDate           = date("Y-m-d", strtotime($date));
$date2             = str_replace('/', '-', $enddate);
$newDate2          = date("Y-m-d", strtotime($date2));
$data['startdate'] = $startdate;
$data['enddate']   = $enddate;
$query             = $this->db->query('SELECT `vno`,`Prdtname`,`Qty`,`bundle` FROM purchaseitem WHERE billdate >= "$newDate"AND billdate <= "$newDate2" ORDER by `vno`')->result_array();
$data['query']     = $query;

View Code:

<?php foreach ($query as $row): ?>
                                            <tr><td></td><td></td><td></td><td></td>
                                        <td><?=$row['Prdtname'];?></td>
                                        <td><?=$row['Qty'];?></td>
                                        <td><?=$row['bundle'];?></td>

                                        <?php endforeach ?> 
dhara
  • 248
  • 1
  • 16
  • can you print your query and post it here? – Yogendrasinh Nov 20 '18 at 07:00
  • Hi! How is yout date stored in the db? Don't you need the H:i:s added to the date? – Eva Nov 20 '18 at 07:02
  • @Yogendrasinh I am getting no values for this query tried in phpmyadmin – dhara Nov 20 '18 at 07:04
  • @Eva i just store the date only – dhara Nov 20 '18 at 07:05
  • Can you show the actual dates your using (both from the database and the ones in `$newDate`) as well as the datatype of the database values. – Nigel Ren Nov 20 '18 at 07:08
  • I am getting the value from the user as dd-mm-yyyy and convert it into yyyy-mm-dd format..And i am using date as datatype and store it in yyyy-mm-dd format – dhara Nov 20 '18 at 07:10
  • Can you see if https://stackoverflow.com/questions/3822648/how-do-i-query-between-two-dates-using-mysql helps. – Nigel Ren Nov 20 '18 at 07:14
  • @dhara I want to look in your printed query. as i have tested i am getting result as below query. SELECT `vno`,`Prdtname`,`Qty`,`bundle` FROM purchaseitem WHERE billdate >= "2018-10-05"AND billdate <= "2018-10-11" ORDER by `vno` – Yogendrasinh Nov 20 '18 at 07:16
  • @NigelRen yes sir..Tried but getting nothing as a result – dhara Nov 20 '18 at 07:22
  • @Yogendrasinh Yes i got the same result as yours..Tried in phpmyadmin – dhara Nov 20 '18 at 07:23

4 Answers4

4

Change your query to below query and check.

You have to pass date in single or doube quote.

$query = $this->db->query('SELECT `vno`,`Prdtname`,`Qty`,`bundle` FROM purchaseitem WHERE billdate >= "'.$newDate.'" AND billdate <= "'.$newDate2.'" ORDER by `vno`')->result_array();

Let me know if still having issue.

Yogendrasinh
  • 895
  • 1
  • 8
  • 23
0

You can use Between

SELECT `vno`,`Prdtname`,`Qty`,`bundle` FROM purchaseitem WHERE billdate BETWEEN "$newDate" AND "$newDate2" ORDER by `vno`
Arup Garai
  • 151
  • 1
  • 7
0

If you store the date only, I would try to use unix timestamp.

$snewsDate = strtotimr($startdate);
$newDate2 = strtotime($enddate);

SELECT `vno`,`Prdtname`,`Qty`,`bundle` FROM purchaseitem WHERE billdate BETWEEN 
UNIX_TIMESTAMP($newDate) AND UNIX_TIMESTAMP($newDate2) ORDER by `vno
Eva
  • 133
  • 1
  • 14
0

Try this one:

$startdate         = $this->input->post('SDate');
$enddate           = $this->input->post('EDate');
$newDate           = date_format(date_create($startdate),"Y-m-d");
$newDate2          = date_format(date_create($enddate),"Y-m-d");
$data['startdate'] = $startdate;
$data['enddate']   = $enddate;
$query             = $this->db->query("SELECT vno,Prdtname,Qty,bundle FROM purchaseitem WHERE billdate BETWEEN '$newDate' AND '$newDate2' ORDER by vno")->result_array();
$data['query']     = $query;
Twinkle
  • 191
  • 2
  • 12