Error convert varchar to int codeigniter sql server

2

Hi, I'm doing a simple application with CodeIgniter and sql 2000, which searches for records according to a client RUT, which is of type varchar.

model:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class Prestaciones_model extends CI_Model
{
    public function getHospitalizaciones($rut){
        $query = $this->db->query('SELECT A.NRO_FI,A.FECHA FROM FICHA A,FIC_PAC B WHERE A.NRO_FI=B.NRO_FI AND B.RUT_NUM='.$rut.' ORDER BY A.FECHA DESC');
        return $query->result();
    }
}

The query is executed in the sql server manager, and if it works, but here I get this error:

  

Severity: Warning

     

Message: odbc_exec (): SQL error: [Microsoft] [ODBC SQL Server   Driver] [SQL Server] Syntax error when converting nvarchar value   '166-97' for a column of data type int., SQL state 22005 in   SQLExecDirect

     

Filename: odbc / odbc_driver.php

     

Line Number: 138

     

Backtrace:

     

File:   C: \ xampp \ htdocs \ Performances \ application \ models \ Performance_model.php   Line: 7 Function: query

     

File: C: \ xampp \ htdocs \ Features \ application \ controllers \ Welcome.php   Line: 17 Function: getHospitalizations

     

File: C: \ xampp \ htdocs \ Performances \ index.php Line: 315 Function:   require_once

Looking at the bd '166-97' is a patient's rut, which is obviously wrong, but with all varchar records of that type I get the same error. But it's only with php since I tried the same in c # and if it works.

Any suggestions to solve this error?

    
asked by gohan2016 27.07.2016 в 23:04
source

2 answers

0

Use the function

  

result_array

function get_info_carpeta($arreglo){
       $sql= "select fechaRegistro,descripcion from carpetas where activo=true and nombreCarpeta='".$arreglo['carpeta']."'";
        $resultado = $this->db->query($sql);
        return $resultado -> result_array();
        }

If you are using APIs then your function in the API would consume the model in the following way:

function consulta_carpeta_post(){
        $arreglo = array('carpeta'=>$this->post('carpeta'));
        $result = $this->Modelo_busqueda->get_info_carpeta($arreglo);
        $this->response($result);
    }

Finally your controller would consume the API in the following way:

$carpeta = $this->input->post('carpeta');
//recibido de la vista
        $ruta = $this->config->site_url() . "/api/Busqueda_api/consulta_carpeta";
        $this->curl->create($ruta);
        $this->curl->post(array('carpeta'=> $carpeta));
        $result['carpeta_info'] = json_decode($this->curl->execute());
        if(!is_null($result['carpeta_info']) && $this->session->userdata('tipo')==1){
           $this->load->view('Header');
            $this->load->view('Menu_comprador');
           $this->load->view('InfoCarpeta',$result);
        }else{
           $this->SinRegistros();
        }

If you are not using REST APIs, then your controller would directly consume the model and the result would be sent to your view.

    
answered by 28.07.2016 в 00:11
0

Check that $ rut is a String and not a number, if so you have to put the sql quotes leaving the query like this:

 $query = $this->db->query("SELECT A.NRO_FI,A.FECHA FROM FICHA A,FIC_PAC B WHERE A.NRO_FI=B.NRO_FI AND B.RUT_NUM='$rut' ORDER BY A.FECHA DESC");
    
answered by 28.07.2016 в 21:58