I am trying to do a query onto my database with the following code.
public function getFreeFields($date){
print_r($date);
$db = Zend_Registry::get('db');
$select = $db->select()
->from(array('f' => $this->_tablename()), array('f.fie_id','f.fie_name'))
->join('reservation','reservation.res_field = f.fie_id')
->where('reservation.res_date_from > ?'. $date)
->where('reservation.res_date_till < ?'. $date);
$result = $db->fetchRow($select);
$data = $this->_mapper->toObject( is_array($result) ? $result : array() );
return $data;
}
But it crashes with the following error.
Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound'
When I die after the print_r, I got the correct date that I give to the request.
Any help?
change this
->where('r.res_date_from > ?'. $date)
->where('r.res_date_till < ?'. $date);
to
->where('r.res_date_from > ?', $date)
->where('r.res_date_till < ?', $date);
Related
My error message in codeigniter framework:
A Database Error Occurred
Error Number: 1140
In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'hms.rooms.id'; this is incompatible with sql_mode=only_full_group_by
SELECT `rooms`.*, count(room_no) as total_rooms FROM `rooms` WHERE `room_type_id` = '10'
Filename: D:/Installed_Apps/OpenServer/OpenServer/domains/hms.loc/system/database/DB_driver.php
Line Number: 691
Why is there such an error and how to eliminate it? In what there can be a problem in the code?
In these functions, something is wrong or everything is normal?
function check_availability($check_in,$check_out,$adults,$kids,$room_type_id){
$query = '?date_from='.$check_in.'&date_to='.$check_out.'&adults='.$adults.'&kids='.$kids.'&room_type=';
$CI =& get_instance();
if($check_in==$check_out){
$check_out = date('Y-m-d', strtotime($check_out.'+ 1 day'));
}
$CI->db->where('id',1);
$settings = $CI->db->get('settings')->row_array();
$CI->db->where('id',$room_type_id);
$CI->db->select('room_types.*,base_price as price');
$room_type = $CI->db->get('room_types')->row_array();
//echo '<pre>'; print_r($room_type);die;
$CI->db->where('room_type_id',$room_type_id);
$CI->db->select('rooms.*,count(room_no) as total_rooms');
$rooms = $CI->db->get('rooms')->row_array();
$total_rooms = $rooms['total_rooms'];
//echo '<pre>'; print_r($rooms);die;
$begin = new DateTime($check_in);
$end = new DateTime($check_out);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach($period as $dt){
$date = $dt->format( "Y-m-d" );
$dayno = $dt->format( "N" );
$day = $dt->format( "D" );
$day = strtolower($day);
///echo $date;die;
//check for room block period
if($date >= $settings['room_block_start_date'] && $date <=$settings['room_block_end_date'])
{
$block_message = "Sorry.. No Room Available Between ".date('d/m/Y',strtotime($settings['room_block_start_date']))." to ".date('d/m/Y',strtotime($settings['room_block_end_date']))." ";
$CI->session->set_flashdata('error', $block_message);
redirect('');
}
$CI->db->where('O.room_type_id',$room_type_id);
$CI->db->where('R.date',$date);
$CI->db->select('R.*,');
$CI->db->join('orders O', 'O.id = R.order_id', 'LEFT');
$orders = $CI->db->get('rel_orders_prices R')->result_array();
//echo '<pre>'; print_r($orders);die;
//echo $total_rooms;die;
if($total_rooms > 0){
//echo count($orders);die;
if(count($orders) >= $total_rooms){
$CI->session->unset_userdata('booking_data');
$CI->session->unset_userdata('coupon_data');
$CI->session->set_flashdata('error', "Sorry.. This Dates Between Rooms Not Available Please Try With Another Date Or Room");
redirect('front/book/index'.$query);
}else{
continue; // continue loop
}
}else{
$CI->session->unset_userdata('booking_data');
$CI->session->unset_userdata('coupon_data');
$CI->session->set_flashdata('error', "Sorry.. This Dates Between Rooms Not Available Please Try With Another Date Or Room");
redirect('front/book/index'.$query);
}
}
return;
}
function check_availability_ajax($check_in,$check_out,$adults,$kids,$room_type_id){
$query = '?date_from='.$check_in.'&date_to='.$check_out.'&adults='.$adults.'&kids='.$kids.'&room_type=';
$CI =& get_instance();
if($check_in==$check_out){
$check_out = date('Y-m-d', strtotime($check_out.'+ 1 day'));
}
$CI->db->where('id',1);
$settings = $CI->db->get('settings')->row_array();
$CI->db->where('id',$room_type_id);
$CI->db->select('room_types.*,base_price as price');
$room_type = $CI->db->get('room_types')->row_array();
//echo '<pre>'; print_r($room_type);die;
$CI->db->where('room_type_id',$room_type_id);
$CI->db->select('rooms.*,count(room_no) as total_rooms');
$rooms = $CI->db->get('rooms')->row_array();
$total_rooms = $rooms['total_rooms'];
//echo '<pre>'; print_r($rooms);die;
$begin = new DateTime($check_in);
$end = new DateTime($check_out);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach($period as $dt){
$date = $dt->format( "Y-m-d" );
$dayno = $dt->format( "N" );
$day = $dt->format( "D" );
$day = strtolower($day);
if($date >= $settings['room_block_start_date'] && $date <=$settings['room_block_end_date'])
{
$block_message = "Sorry.. No Room Available Between ".date('d/m/Y',strtotime($settings['room_block_start_date']))." to ".date('d/m/Y',strtotime($settings['room_block_end_date']))." ";
return $block_message;
}
$CI->db->where('O.room_type_id',$room_type_id);
$CI->db->where('R.date',$date);
$CI->db->select('R.*,');
$CI->db->join('orders O', 'O.id = R.order_id', 'LEFT');
$orders = $CI->db->get('rel_orders_prices R')->result_array();
//echo $total_rooms;die;
if($total_rooms > 0){
if(count($orders) > $total_rooms){
$CI->session->unset_userdata('booking_data');
$CI->session->unset_userdata('coupon_data');
return 'Sorry.. This Dates Between Rooms Not Available Please Try With Another Date Or Room';
}else{
continue; // continue loop
}
}else{
$CI->session->unset_userdata('booking_data');
$CI->session->unset_userdata('coupon_data');
return 'Sorry.. This Dates Between Rooms Not Available Please Try With Another Date Or Room';
}
}
return 1;
}
Here is my Book.php controller code where I use that functions:
function index()
{
//echo '<pre>'; print_r($_GET);
//check availbilty
//get_invoice_number();
$this->session->unset_userdata('booking_data');
$this->session->unset_userdata('coupon_data');
$data['page_title'] = lang('make_reservation');
$data['meta_description'] = $this->setting->meta_description;
$data['meta_keywords'] = $this->setting->meta_keywords;
$data['banners'] = $this->homepage_model->get_banners();
$data['testimonials'] = $this->homepage_model->get_testimonials(); // get 6 testimonials
$data['room_types'] = $this->homepage_model->get_room_types_all();
$data['taxes'] = $this->homepage_model->get_taxes();
if(!empty($_GET['room_type'])){
$data['services'] = $this->homepage_model->get_paid_services($_GET['room_type']);
}
//echo '<pre>'; print_r($data['services']);
if(empty($_GET['room_type'])){
$this->render('book/room_types', $data);
}else{
check_availability($_GET['date_from'],$_GET['date_to'],$_GET['adults'],$_GET['kids'],$_GET['room_type']);
$data['room_type'] = $this->homepage_model->get_room_type($_GET['room_type']);
$this->render('book/view', $data);
}
}
you are facing this problem because of the ONLY_FULL_GROUP_BY option in the MYSQL so kindly set,
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
to solve the issue.
SQL query including aggregate functions like COUNT() or SUM() etc. always have a GROUP BY clause in it. Which specifies the other non-grouped columns in the final resultset.
In you query the following remarks are noted:
You have specified rooms.* which is not recommended while grouping.
You may mention specific columns while grouping, and specify those columns in the GROUP BY clause too.
For example,
SELECT
Count(product_tb.product_id),
product_tb.`name`,
product_tb.details
FROM
`product_tb`
WHERE
product_tb.product_id = 1
GROUP BY
product_tb.`name`,
product_tb.details
I have the following SQL procedure which works on SQL Server Management studio when I execute it
USE [DatabaseName]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[getRegisterSummaryReport]
#registerID = -1,
#StartBusinessDate = '1900-01-01 00:00:00',
#EndBusinessDate = '3000-01-01 00:00:00'
SELECT 'Return Value' = #return_value
GO
what I want now is to pass the StartBusinessDate and the EndBusinessDate from a PHP function ( sorry I'm very new to PHP) .
here's what I had but for some reason I get false as a respond ( I think I'm not using the proper way to pass the date to my SQL Server).
public static function getRegisterSummary($registerId, $startBusinessDate, $endBusinessDate, $dbConnection){
$date_start = '1900-01-01 00:00:00';
$date_end = '3000-01-01 00:00:00';
if($startBusinessDate == "") $startBusinessDate = date('Y-m-d G:i:s', $date_start); // if startBusinessDate is "" I want the replace it with date_start
if($endBusinessDate == "") $endtBusinessDate = date('Y-m-d G:i:s', $date_end);
$sql = "exec [database1].[dbo].[getRegisterSummaryReport] #registerId = {$registerId}, #startBusinessDate = '{$startBusinessDate}', #endBusinessDate = '{$endBusinessDate}'";
$results = $dbConnection->query($sql);
if($results){
return $results;
}
return false;
}
Thank you
Ok This is embarrassing!!
first of all I changed the syntax to this
if($startBusinessDate == "") $startBusinessDate = date('1900-01-01');
if($endBusinessDate == "") $endBusinessDate = date('3000-01-01');
for some reason this worked with me and passed the correct value unlike when I added
date('Y-m-d G:i:s', $date_start);
Also I made a mistake in
if($endBusinessDate == "") $endtBusinessDate = date('Y-m-d G:i:s', $date_end);
I added t after $endtBusinessDate :(
hello i need help please. mi code is error sorry for my bad englihs.
error : Fatal error: Call to a member function format() on boolean in C:\xampp\htdocs\aplicacion\application\views\guest\container.php on line 11
My container.php
My intention is to get the date of creation of the blog. Be added to the url of my website. And also the name of the post
<?php
foreach ($consulta->result() as $fila) {
?>
<div class="post-preview">
<?php
$date = DateTime::createFromFormat('YYYY-MM-DD', $fila->fecha);
$year = $date->format('Y',$date);
$name = str_replace('','_', $fila->post);
?>
<a href="<?php echo base_url()?>article/post/<?php echo $year ?>/<?php echo $name ?>">
article.php - controller
class article extends CI_Controller
{
public function post($year , $name)
{
// $fila = $this->post->getPostByName($id);
$fila = $this->post->getPostByYearAndName($year , $name);
if($fila == null){
echo "ERROR";
return;
}
$data = array('titulo' => $fila->post);
$this->load->view("guest/head" , $data);
$data = array('app' => 'blog');
$this->load->view("guest/nav" , $data);
$data = array(
'post' => $fila->post,
'descripcion' => $fila->descripcion,
'img' =>$fila->img);
$this->load->view("guest/header" , $data);
$data = array('contenido' => $fila->contenido);
$this->load->view("/guest/post" , $data);
$this->load->view("guest/footer");
}
post.php - MODEL
public function getPostByYearAndName($year = '', $name = '')
{
$result = $this->db->query("SELECT * FROM post WHERE year(fecha) = '$year' AND post LIKE '$name'");
return $result->row();
}
Your code works:
$date = DateTime::createFromFormat('y-m-d', '17-03-12');
echo $date->format('Y');
->
2017
if $date is false it means that your input $fila->fecha is not at the format YY-MM-DD.
If for example, your date is at the format YYYY-MM-DD you will have to use 'Y-m-d' instead.
Update:
Just to illustrate, if the input is a date but not at the specified format, for example:
$date = DateTime::createFromFormat('y-m-d', '2017-03-12');
echo $date->format('Y');
It reproduces OPs bug:
PHP Fatal error: Uncaught Error: Call to a member function format() on boolean in ~/tmp/date.php:5
So either $fila->fecha is empty, not a date or not at the format YY-MM-DD
Help me, i wanna put the timestone to this format Hour:minutes:secondes
$services = $pdo ->prepare("SELECT * FROM `servicos` WHERE `DE` = :u");
$services->bindValue(":u", "1");
$services->execute();
echo $numServico = $services->rowCount(); // Contagem de serviços
$l = $services->fetchAll(PDO::FETCH_OBJ);
//$json = json_encode($l);
echo '{"data":[';
//echo $json;
foreach ($l as $listar) {
$inicioHora = new DateTime("listar->REGISTRO");
$inicioHora = $inicioHora->format('H:i:s');
}
But i receive this
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (listar->REGISTRO) at position 0 (l): The timezone could not be found in the database' on line 26
if the field $listar->REGISTRO is a timestamp, then you only need to use the date() function http://php.net/manual/en/function.date.php
$time = date('H:i:s', $listar->REGISTRO);
Replace this:
$inicioHora = new DateTime("listar->REGISTRO");
$inicioHora = $inicioHora->format('H:i:s');
With this:
$inicioHora = date('H:i:s', strtotime($listar->REGISTRO));
public function getVoters($limit = 5)
{
$db = $this->load->database("cms", true);
$year = date('Y');
$month = date('n');
$res = $this->db->query("SELECT account, counter FROM votecounter WHERE year=$year AND month=$month ORDER BY counter DESC LIMIT 5");
$x = 1;
while ($arr = $res->fetch())
{
$accid = $arr['account'];
$res2 = $DB->prepare("SELECT `displayName` FROM `account_data` WHERE `id` = :acc LIMIT 1;");
$res2->bindParam(':acc', $accid, PDO::PARAM_STR);
$res2->execute();
$arr2 = $res2->fetch();
echo '
<li>
<p>', $x ,'</p>
', $arr2['displayName'] ,'
<span>', $arr['counter'] ,' <i>Votes</i></span>
</li>';
$x++;
unset($res2, $arr2, $accid);
}
unset($res, $x);
}
This used to work on a PHP site I had, however I changed to a new website built with Smarty/Codeigniter and this error has been showing up and I have 0 idea how to fix it.
Fatal error: Call to undefined method CI_DB_mysqli_result::fetch()
Any help would be appreciated..
When I'm not mistaken there's no fetch() function in codeigniter.
So maybe use result_array() instead.