Php 使用错误列中的数据进行查询

Php 使用错误列中的数据进行查询,php,sql,sql-server,Php,Sql,Sql Server,这个标题可能有点误导,因为我不知道确切的措辞。我有一个PHP页面,其中显示了带有搜索过滤器的记录列表。代码如下所示。页面显示正确,但在其中一列中,我发现MS SQL的以下数据库错误 Error Number: 22018 [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting the varchar value 'Marie Stopes International' to

这个标题可能有点误导,因为我不知道确切的措辞。我有一个PHP页面,其中显示了带有搜索过滤器的记录列表。代码如下所示。页面显示正确,但在其中一列中,我发现MS SQL的以下数据库错误

Error Number: 22018

[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting the varchar value 'Marie Stopes International' to data type int.

SELECT * FROM ctbl_mas_funder WHERE flag = 0 AND id = 'Marie Stopes International'

Filename: C:\inetpub\wwwroot\FMS\system\database\DB_driver.php

Line Number: 330
我认为这是因为它不是从查询中提取ID,而是从其他列中提取数据,但我不能完全确定是什么导致了这种情况

该页面的代码如下所示

<link href="<?php echo base_url(); ?>assets/js/plugins/forms/select2/select2.css" rel="stylesheet" />
<script src="<?php echo base_url(); ?>assets/js/plugins/forms/uniform/jquery.uniform.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/plugins/forms/select2/select2.js"></script>
<script src="<?php echo base_url(); ?>assets/js/plugins/tables/datatables/jquery.dataTables.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/plugins/tables/datatables/jquery.dataTables.columnFilter.js"></script>
<script src="<?php echo base_url(); ?>assets/js/app.js"></script><!-- Core js functions -->

<script src="<?php echo base_url(); ?>assets/js/pages/data-tables.js"></script><!-- Init plugins only for page -->



<div class="wrapper">
  <div class="crumb">
    <ul class="breadcrumb">
      <li><a href="#"><i class="icon16 i-home-4"></i>Home</a></li>
      <li><a href="#">Manage Funding</a></li>
      <li class="active">
        <?=$title ?>
      </li>
    </ul>
  </div>
  <div class="container-fluid">
    <div id="heading" class="page-header">
      <h1><i class="icon20 i-table-2"></i>
        <?=$title ?>
      </h1>
    </div>
    <div class="row">
      <div class="col-lg-12">
        <div class="panel panel-default">
          <div class="panel-heading">
            <div class="icon"><i class="icon20 i-table"></i></div>
            <h4>
              <?=$title ?>
            </h4>
            <a href="#" class="minimize"></a>
            <div class="flexigrid" style="width: 100%;">
              <div class="tDiv">
                <div class="tDiv2">
                  <?php 
                  $f_manange_funding=explode(',', $this->session->userdata('f_manange_funding'));

if (in_array('6', $m_projects) or $this->session->userdata('user_type')=='admin') 
{?>
                  <!--<div class="fbutton tip" title="Download Word" onclick="javascript:document.location.href='<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/download_word'"><div><span class="word" style="padding-left: 20px;">Word</span></div></div>
                    <div class="btnseparator"></div>-->
                  <?php }
if (in_array('5', $m_projects) or $this->session->userdata('user_type')=='admin') 
{?>
                  <!--<div class="fbutton tip" title="Download Excel" onclick="javascript:document.location.href='<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/download_excel'"><div><span class="excel" style="padding-left: 20px;">Excel</span></div></div> <div class="btnseparator"></div> -->
                  <?php }
if (in_array('7', $m_projects) or $this->session->userdata('user_type')=='admin') 
{?>
                  <!--<div class="fbutton tip" title="Download Pdf" onclick="javascript:document.location.href='<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/download_pdf'"><div><span class="pdf" style="padding-left: 20px;">Pdf</span></div></div>
                        <div class="btnseparator"></div>-->
                  <?php } 
if (in_array('8', $m_projects) or $this->session->userdata('user_type')=='admin') 
{?>
                  <!--<div class="fbutton tip" title="Print" onclick="javascript:window.open('<?php echo site_url("office").'/'.$this->uri->segment(2).'/'.$this->uri->segment(3); ?>/print_page', 'mywindow', 'location=1,status=1,scrollbars=1,  width=600,height=600')"><div><span class="print" style="padding-left: 20px;">Print</span></div></div>
                        <div class="btnseparator"></div>-->
                  <?php } ?>
                  <div class="fbutton tip" title="Help"   >
                    <div><span class="help_ic" style="padding-left: 20px;"><a href="#myModal"   data-toggle="modal">Help</a></span></div>
                  </div>
                </div>
                <div style="clear:both"></div>
              </div>
            </div>
          </div>
          <!-- End .panel-heading -->
          <div class="panel-body">
            <?php
      //flash messages

        if($this->session->flashdata('feedback'))
        {
          echo '<div class="alert alert-success">';
            echo '<a class="close" data-dismiss="alert">×</a>';
            echo $this->session->flashdata('feedback');
          echo '</div>';       
        } ?>

            <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered table-hover"  id="dataTable">
              <thead>
                <tr>
                  <th > Funding Hierarchy</th>
                  <th >Project Code </th>
                  <th >Country of implementation</th>
                  <th >Source of funding </th>
                  <th > Primary Donor</th>
                  <th > Contracting donor</th>
                  <th > Contracting recipient </th>
                  <th >Short  Title</th>
                  <th >Funding status</th>
                  <th width="8%">Action</th>
                </tr>
              </thead>
              <tbody>
                <?php
               $str="";

    if(($funding_mechanism !="")  or ($primary_donor !="")    or ($contracting_donor_funder_code !="" ) or ($funder_code_primary !="" )     or ($funding_manager !="" ) ){




               if($fundingstatus !="")
               {



                 $str=$str."ctbl_contract_funding_info.fundingstatus ='$fundingstatus'";
               }

                 if($project_code !="")
               {

                    if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_info.project_code='$project_code'";
               }




                if($fundingHierarchy !="")
               {
                    if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_info.fundingHierarchy='$fundingHierarchy'";
               }



                if($funding_mechanism !="")
               {
                    if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_fin.funding_mechanism='$funding_mechanism'";
               }



                if($subfunding_title !="")
               {

                   if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_info.subfunding_title='$subfunding_title'";
               }




                if($primary_donor !="")
               {

                   if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_info.primary_donor='$primary_donor'";
               }

                 if($source_funding !="")
               {

                   if($str!="")
                   {
                      $str=$str." and ";  
                   }
                // $str=$str."ctbl_contract_funding_info.source_funding='$source_funding'";

                    $str=$str."ctbl_contract_funding_info.source_funding  REGEXP '.*;s:[0-9]+:\"$source_funding\".*'";
               }




                 if($contracting_donor !="")
               {
                    if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_info.contracting_donor='$contracting_donor'";
               }



                if($contracting_recipient !="")
               {
                    if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_info.contracting_recipient='$contracting_recipient'";
               }

                if($funder_code_source !="")
               {
                    if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_info.funder_code_source='$funder_code_source'";
               }


               if($contracting_donor_funder_code !="")
               {
                    if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_info.contracting_donor_funder_code='$contracting_donor_funder_code'";
               }



                 if($funder_code_primary !="")
               {
                    if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_info.funder_code_primary='$funder_code_primary'";
               }  






               if($country_implemention !="")
               {
                    if($str!="")
                   {
                      $str=$str." and ";  
                   }
                // $str=$str."ctbl_contract_funding_info.country_implemention='$country_implemention'";

                 $str=$str."ctbl_contract_funding_info.country_implemention  REGEXP '.*;s:[0-9]+:\"$country_implemention\".*'";
               }


                if($funding_manager !="")
               {
                    if($str!="")
                   {
                      $str=$str." and ";  
                   }
                 $str=$str."ctbl_contract_funding_mgmt.funding_manager='$funding_manager'";
               }





            $query_search = $this->db->query('SELECT ctbl_contract_funding_info.* ,ctbl_contract_funding_mgmt.funding_manager,ctbl_contract_workflow.id as id1,ctbl_contract_workflow.family_id as main_id ,ctbl_contract_workflow.parent_type 
FROM ctbl_contract_workflow
LEFT JOIN ctbl_contract_funding_info ON ctbl_contract_funding_info.contract_id = ctbl_contract_workflow.id 
LEFT JOIN ctbl_contract_funding_mgmt ON ctbl_contract_funding_mgmt.contract_id = ctbl_contract_funding_info.contract_id

LEFT JOIN ctbl_contract_funding_fin ON ctbl_contract_funding_fin.contract_id = ctbl_contract_funding_mgmt.contract_id
WHERE   '.$str.' 
ORDER BY ctbl_contract_workflow.id asc');
            $total=$query_search->num_rows() ;
            foreach($query_search->result_array() as $row_search)
            {



                echo '<tr>';
                echo '<td>'.$row_search['fundingHierarchy'].'</td>';

                if($row_search['project_code']!='')
                {
                echo '<td>E'.$row_search['project_code'].'</td>';
                }
                else
                {
                echo '<td></td>';   
                }

                if($row_search['country_implemention']=='')
                {


            $data['list1'] = $this->common_model->get_by_id('id',$country_implemention,'ctbl_mas_country');




                echo '<td>'.$data['list1'][0]['name'].'</td>';

                }
                else
                {
                    ?>
              <td><?php $query = $this->db->query('SELECT * FROM ctbl_mas_country order by rank desc');
    $total=$query->num_rows() ;
    if($total>0)
    {
    foreach ($query->result_array() as $row)
    {

        if(unserialize($row_search['country_implemention'])!='')
                {
                $implementing_agency= unserialize($row_search['country_implemention']);
                }
                else
                {
                $implementing_agency=array();
                }



                if (in_array($row['id'], $implementing_agency))
                {
                 echo $row['name']."<br>";  
                }
    }
    }
    ?></td>
                <?php 



                }


                if($source_funding!='' || $source_funding!='Array')
                {


            $data['list'] = $this->common_model->get_by_id('id',$source_funding,'ctbl_mas_funder');

                echo '<td>'.$data['list'][0]['name'].'</td>';




                }

                else
                {


                    ?>
                <td><?php
    $query = $this->db->query('SELECT * FROM ctbl_mas_funder Where flag=0 ');
    $total=$query->num_rows() ;
    if($total>0)
    {
    foreach ($query->result_array() as $row)
    {

        if(unserialize($row_search['source_funding'])!='')
                {
                $source_funding= unserialize($row_search['source_funding']);
                }
                else
                {
                $source_funding=array();
                }



                if (in_array($row['id']."-".$row['code'], $source_funding))
                {
                 echo $row['name']."<br>";
                }




    }
    }
    ?></td>
                <?php 

                }

                $data['list1'] = $this->common_model->get_by_id('id',substr($row_search['primary_donor'],0, strpos($row_search['primary_donor'], "-")),'ctbl_mas_funder');
                echo '<td>'.$data['list1'][0]['name'].'</td>';
                ?>
                <td><?php 

                                                $c=explode("-",$row_search['contracting_donor']);

                                                if(is_array($c) && $c[1]!='')
                                                {


                                                $data['list2'] = $this->common_model->get_by_id('id',$c[0],'ctbl_mas_funder');
                                                    echo $data['list2'][0]['name'];

                                                }
                                                else
                                                {
                                                    echo  $row_search['contracting_donor'];
                                                }

                                                 ?></td>
                <td><?php  




  if($row_search['contracting_recipient']!='Select')
                                                  {
                                                      if($row_search['type_recipient']!='Select')
                                                  {
    if($row_search['type_recipient']=='MSI partnership')
    {

    if($row_search['contracting_recipient']!='Select')
    {
         $data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_msi_entities');
         $d=$data['list_cd'][0]['name'] ;
    }
    else
    {
        $d='';
    }
        ?>
                  <?php echo $d ; ?>
                  <?php     
    }
    else if($row_search['type_recipient']=='External partner')
    {

    if($row_search['contracting_recipient']!='Select')
    {
         $data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_external_partners');
         $d=$data['list_cd'][0]['name'] ;
    }
    else
    {
        $d='';
    }
        ?>
                  <?php echo $d ; ?>
                  <?php     
    }
    else if($row_search['type_recipient']=='MSI funding stream')
    {

    if($row_search['contracting_recipient']!='Select')
    {
         $data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_msi_funding_streams');
         $d=$data['list_cd'][0]['name'] ;
    }
    else
    {
        $d='';
    }
        ?>
                  <?php echo $d ; ?>
                  <?php     
    }
    else if($row_search['type_recipient']=='MSI funding stream - project donor')
    {

    if($row_search['contracting_recipient']!='Select')
    {
         $data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_funder');
         $d=$data['list_cd'][0]['name'] ;
    }
    else
    {
        $d='';
    }
        ?>
                  <?php echo $d ; ?>
                  <?php     
    }


                                                  }}?></td>
                <?php 
                echo '<td>'.$row_search['subfunding_title'].'</td>';

                echo '<td>'.$row_search['fundingstatus'].'</td>';
                echo '<td class="crud-actions">';
                 if($row_search['main_id']==$row_search['id1'])
                 {
                echo '<a href="'.site_url("office").'/master/create_entry/view/?contract_id='.$this->encryption->encode($row_search['id1']).'" class="tip"   title="View Details"><i class="icon16  i-zoom-in"></i>  </a>';
                 }
                 else
                 {
                echo '<a href="'.site_url("office").'/master/create_entry/view_sub_funding/?contract_id='.$this->encryption->encode($row_search['id1']).'&main_id='.$this->encryption->encode($row_search['main_id']).'" class="tip"   title="View Details"><i class="icon16  i-zoom-in"></i>  </a>';

                 }

                //echo '<a href="'.site_url("office").'/master/create_entry/update/?contract_id='.$this->encryption->encode($row_search['id']).'" class="tip" title="Edit"><i class="icon16 i-pencil"></i> </a>';  

                //echo '<a  class="tip" title="Delete" href="'.site_url("office").'/master/create_entry/delete/?contract_id='.$this->encryption->encode($row_search['id']).'"  title="Remove"><i class="icon16 i-remove"></i></a>';                   
                echo ' </td></tr>';
               }
            /// to show no records

            /// to show no records

             }else{
//// searching results query ends here  
//// searching results query echo ends here        



              $query_search = $this->db->query('SELECT ctbl_contract_funding_info.* ,ctbl_contract_funding_mgmt.funding_manager,ctbl_contract_workflow.id as id1,ctbl_contract_workflow.family_id as main_id,ctbl_contract_workflow.parent_type 
FROM ctbl_contract_workflow
LEFT JOIN ctbl_contract_funding_info ON ctbl_contract_funding_info.contract_id = ctbl_contract_workflow.id 
LEFT JOIN ctbl_contract_funding_mgmt ON ctbl_contract_funding_mgmt.contract_id = ctbl_contract_workflow.id

LEFT JOIN ctbl_contract_funding_fin ON ctbl_contract_funding_fin.contract_id = ctbl_contract_funding_mgmt.contract_id

ORDER BY ctbl_contract_workflow.id asc');
            $total=$query_search->num_rows() ;
            foreach($query_search->result_array() as $row_search)
              {


                echo '<tr>';
                echo '<td>'.$row_search['fundingHierarchy'].'</td>';
                if($row_search['project_code']!='')
                {
                echo '<td>E'.$row_search['project_code'].'</td>';
                }
                else
                {
                    echo '<td>'.$row_search['project_code'].'</td>';
                }
                ?>
                <td><?php $query = $this->db->query('SELECT * FROM ctbl_mas_country order by rank desc');
    $total=$query->num_rows() ;
    if($total>0)
    {
    foreach ($query->result_array() as $row)
    {

        if(unserialize($row_search['country_implemention'])!='')
                {
                $implementing_agency= unserialize($row_search['country_implemention']);
                }
                else
                {
                $implementing_agency=array();
                }



                if (in_array($row['id'], $implementing_agency))
                {
                 echo $row['name']."<br>";  
                }
    }
    }
    ?></td>
                <td><?php
    $query = $this->db->query('SELECT * FROM ctbl_mas_funder Where flag=0 ');
    $total=$query->num_rows() ;
    if($total>0)
    {
    foreach ($query->result_array() as $row)
    {

        if(unserialize($row_search['source_funding'])!='')
                {
                $source_funding= unserialize($row_search['source_funding']);
                }
                else
                {
                $source_funding=array();
                }

                if (in_array($row['id']."-".$row['code'], $source_funding))
                {
                 echo $row['name']."<br>";
                }




    }
    }
    ?></td>
                <?php 
                $data['list1'] = $this->common_model->get_by_id('id',substr($row_search['primary_donor'],0, strpos($row_search['primary_donor'], "-")),'ctbl_mas_funder');
                echo '<td>'.$data['list1'][0]['name'].'</td>';
                    ?>
                <td>
                                                <?php 
                                                //echo  $row['contracting_donor'];
                                                $code_id =explode("-",$row_search['contracting_donor']);



                                                if(trim($row_search['fundingHierarchy']) == 'Donor Funding'){

                                                $data['donor_name'] = $this->common_model->get_by_id('id',$code_id[0],'ctbl_mas_funder');
                                                echo $data['donor_name'][0]['name'];

                                                }
                                                else{
                                                    echo  $row_search['contracting_donor'];
                                                }
                                                 ?>

                                                 </td>

                <td> 
                            <?php                                                  
                            if($row_search['contracting_recipient']!='Select')
                            {
                            if($row_search['type_recipient']!='Select')
                            {                                                       


                            if($row_search['type_recipient']=='MSI partnership')
                            {

                            if($row_search['contracting_recipient']!='Select')
                            {
                            $data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_partnership');
                            $d=$data['list_cd'][0]['name'] ;
                            }
                            else
                            {
                            $d='';
                            }
                            ?>
                            <?php echo $d ; ?>
                            <?php   
                            }
                            else if($row_search['type_recipient']=='External partner')
                            {

                            if($row_search['contracting_recipient']!='Select')
                            {
                            $data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_external_partners');
                            $d=$data['list_cd'][0]['name'] ;
                            }
                            else
                            {
                            $d='';
                            }
                            ?>
                            <?php echo $d ; ?>
                            <?php   
                            }
                            else if($row_search['type_recipient']=='MSI funding stream')
                            {

                            if($row_search['contracting_recipient']!='Select')
                            {
                            $data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_msi_funding_streams');
                            $d=$data['list_cd'][0]['name'] ;
                            }
                            else
                            {
                            $d='';
                            }
                            ?>
                            <?php echo $d ; ?>
                            <?php   
                            }
                            else if($row_search['type_recipient']=='MSI funding stream - project donor')
                            {

                            if($row_search['contracting_recipient']!='Select')
                            {
                            $data['list_cd'] = $this->common_model->get_by_id('id',$row_search['contracting_recipient'],'ctbl_mas_funder');
                            $d=$data['list_cd'][0]['name'] ;
                            }
                            else
                            {
                            $d='';
                            }
                            ?>
                            <?php echo $d ; ?>
                            <?php   
                            }


                            }
                            }

                            ?>                                                   
                    </td>


                <?php 
                echo '<td>'.$row_search['subfunding_title'].'</td>';

                echo '<td>'.$row_search['fundingstatus'].'</td>';
                echo '<td class="crud-actions">';
                  if($row_search['main_id']==$row_search['id1'])
                 {
                echo '<a href="'.site_url("office").'/master/create_entry/view/?contract_id='.$this->encryption->encode($row_search['id1']).'" class="tip"   title="View Details"><i class="icon16  i-zoom-in"></i>  </a>';
                 }
                 else
                 {
                echo '<a href="'.site_url("office").'/master/create_entry/view_sub_funding/?contract_id='.$this->encryption->encode($row_search['id1']).'&main_id='.$this->encryption->encode($row_search['main_id']).'" class="tip"   title="View Details"><i class="icon16  i-zoom-in"></i>  </a>';

                 }

                //echo '<a href="'.site_url("office").'/master/create_entry/update/?contract_id='.$this->encryption->encode($row_search['id']).'" class="tip" title="Edit"><i class="icon16 i-pencil"></i> </a>';  

                //echo '<a  class="tip" title="Delete" href="'.site_url("office").'/master/create_entry/delete/?contract_id='.$this->encryption->encode($row_search['id']).'"  title="Remove"><i class="icon16 i-remove"></i></a>';

                echo ' </td></tr>';
               }

             }
              ?>
                  </tbody>
            </table>
            <?php  require_once('help.php');?>
            <div class="form-group">
                <div class="col-lg-offset-4">
                  <div class="pad-left15"> <a href="<?php echo site_url("office")."/master/funding_management"; ?>" class="btn btn-default tip" title="Cancel"   rel="widget-content"><i class="i-cancel"></i>Cancel/Back</a> </div>
                </div>
              </div>

          </div>
          <!-- End .panel-body --> 
        </div>
        <!-- End .widget --> 
      </div>
      <!-- End .col-lg-12  --> 
    </div>
    <!-- End .row-fluid  --> 

  </div>
  <!-- End .container-fluid  --> 
</div>

id
列很可能是
int
类型。因此,当引擎运行查询时,它需要将该子句的右侧求值为
int
;因此,不能将字符串
'Marie Stopes International'
转换为int,因此会出现错误。您应该使用数字ID,或者查看包含
varchar
名称的列。Parry,您在哪一行有错误?
ID
列很可能是
int
类型。因此,当引擎运行查询时,它需要将该子句的右侧求值为
int
;因此,不能将字符串
'Marie Stopes International'
转换为int,因此会出现错误。您应该使用数字ID或查看包含
varchar
name.Parry的列,哪一行有错误?