2012-06-11 1 views
1

을 실행할 때 . 주먹은 저에게 오류를주지 않으며 그것은 정상적으로 데이터를 제공합니다. 기타 오류가 발생했습니다.정의되지 않은 인덱스는 내가 PHP에서이 쿼리를 실행하면 오류를 얻고 SQL 문을

어떤 아이디어가 잘못 되었나요? 그리고 내가 바꿀 필요가있는 것은 무엇입니까?

편집 : 이 쿼리는 DataTables 스크립트를 사용할 때 사용됩니다. 이 스크립트를 사용하는 경우 그래서 완전히 PHP 코드는 다음입니다 :

<?php 
    /* 
    * Script: DataTables server-side script for PHP and MySQL 
    * Copyright: 2010 - Allan Jardine 
    * License: GPL v2 or BSD (3-point) 
    */ 

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * Easy set variables 
    */ 

    /* Array of database columns which should be read and sent back to DataTables. Use a space where 
    * you want to insert a non-database field (for example a counter or static image) 
    */ 
    $aColumns = array('hit.timestamp','hit.id', 'config.Name', 'hit.meter_id','levels.LevelName','pos.sm_pos','hit.hit_value' ); 

    /* Indexed column (used for fast and accurate table cardinality) */ 
    $sIndexColumn = "hit.id"; 

    /* DB table to use */ 
    $sTable = "hit 
     INNER JOIN config 
       ON hit.id = config.id 
     INNER JOIN levels 
       ON hit.meter_id = levels.id 
     INNER JOIN POS 
       ON pos.id = hit.id 
     INNER JOIN controllers 
       ON pos.controller_id = controllers.id"; 

    /* Database connection information */ 
    $gaSql['user']  = ""; 
    $gaSql['password'] = ""; 
    $gaSql['db']   = ""; 
    $gaSql['server']  = "localhost"; 


    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    * If you just want to use the basic configuration for DataTables with PHP server-side, there is 
    * no need to edit below this line 
    */ 

    /* 
    * MySQL connection 
    */ 
    $gaSql['link'] = mysql_pconnect($gaSql['server'], $gaSql['user'], $gaSql['password'] ) or 
     die('Could not open connection to server'); 

    mysql_select_db($gaSql['db'], $gaSql['link']) or 
     die('Could not select database '. $gaSql['db']); 


    /* 
    * Paging 
    */ 
    $sLimit = ""; 
    if (isset($_GET['iDisplayStart']) && $_GET['iDisplayLength'] != '-1') 
    { 
     $sLimit = "LIMIT ".mysql_real_escape_string($_GET['iDisplayStart']).", ". 
      mysql_real_escape_string($_GET['iDisplayLength']); 
    } 


    /* 
    * Ordering 
    */ 
    $sOrder = ""; 
    if (isset($_GET['iSortCol_0'])) 
    { 
     $sOrder = "ORDER BY "; 
     for ($i=0 ; $i<intval($_GET['iSortingCols']) ; $i++) 
     { 
      if ($_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true") 
      { 
       $sOrder .= $aColumns[ intval($_GET['iSortCol_'.$i]) ]." 
        ".mysql_real_escape_string($_GET['sSortDir_'.$i]) .", "; 
      } 
     } 

     $sOrder = substr_replace($sOrder, "", -2); 
     if ($sOrder == "ORDER BY") 
     { 
      $sOrder = ""; 
     } 
    } 


    /* 
    * Filtering 
    * NOTE this does not match the built-in DataTables filtering which does it 
    * word by word on any field. It's possible to do here, but concerned about efficiency 
    * on very large tables, and MySQL's regex functionality is very limited 
    */ 
    $sWhere = ""; 
    if (isset($_GET['sSearch']) && $_GET['sSearch'] != "") 
    { 
     $sWhere = "WHERE ("; 
     for ($i=0 ; $i<count($aColumns) ; $i++) 
     { 
      if (isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true") 
      { 
       $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch'])."%' OR "; 
      } 
     } 
     $sWhere = substr_replace($sWhere, "", -3); 
     $sWhere .= ')'; 
    } 

    /* Individual column filtering */ 
    for ($i=0 ; $i<count($aColumns) ; $i++) 
    { 
     if (isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '') 
     { 
      if ($sWhere == "") 
      { 
       $sWhere = "WHERE "; 
      } 
      else 
      { 
       $sWhere .= " AND "; 
      } 
      $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' "; 
     } 
    } 


    /* 
    * SQL queries 
    * Get data to display 
    */ 
    $sQuery = " 
     SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))." 
     FROM $sTable 
     $sWhere 
     $sOrder 
     $sLimit 
    "; 
    $rResult = mysql_query($sQuery, $gaSql['link']) or die(mysql_error()); 

    /* Data set length after filtering */ 
    $sQuery = " 
     SELECT FOUND_ROWS() 
    "; 
    $rResultFilterTotal = mysql_query($sQuery, $gaSql['link']) or die(mysql_error()); 
    $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal); 
    $iFilteredTotal = $aResultFilterTotal[0]; 

    /* Total data set length */ 
    $sQuery = " 
     SELECT COUNT(".$sIndexColumn.") 
     FROM $sTable 
    "; 
    $rResultTotal = mysql_query($sQuery, $gaSql['link']) or die(mysql_error()); 
    $aResultTotal = mysql_fetch_array($rResultTotal); 
    $iTotal = $aResultTotal[0]; 


    /* 
    * Output 
    */ 
    $output = array(
     "sEcho" => intval($_GET['sEcho']), 
     "iTotalRecords" => $iTotal, 
     "iTotalDisplayRecords" => $iFilteredTotal, 
     "aaData" => array() 
    ); 

    while ($aRow = mysql_fetch_array($rResult)) 
    { 
     $row = array(); 
     for ($i=0 ; $i<count($aColumns) ; $i++) 
     { 
      if ($aColumns[$i] == "version") 
      { 
       /* Special output formatting for 'version' column */ 
       $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ]; 
      } 
      else if ($aColumns[$i] != ' ') 
      { 
       /* General output */ 
       $row[] = $aRow[ $aColumns[$i] ]; 
      } 
     } 
     $output['aaData'][] = $row; 
    } 

    echo json_encode($output); 
?> 

$row[] = $aRow[ $aColumns[$i] ];을 사용하고 출력하기 나는이 생각이 마지막 부분은 내가 선택 성명에는 table.column 형식을 사용하고 같은 문제가있는 곳입니다. 그리고 재미있는 점은 select 문에서 내 첫 번째 열이 물마루가 가고 값을 가져 오는 것입니다. 그리고 다른 사람들이 나에게 정의되지 않은 색인을 제공하고 있습니다.

+1

은 당신의 SQL 쿼리가 직접 PHP 오류에 연결되어 있지 않습니다. 정의되지 않은 색인 오류를 제공하는 PHP 코드를 표시하고 전체 오류 메시지를 표시하십시오. – Repox

+0

그냥'Undefined index : hit.id'라고 쓰여진 다음 모든 열에 똑같이 쓰여집니다. –

+1

주위의 PHP 코드를 붙여 넣는 것을 잊었습니다. – Repox

답변

2

이 문제는 modified version의 DataTables 예제 스크립트와 추가로 discussion on SO chat을 사용하여 해결되었습니다.

나는 시간이 있 자마자 미래의 방문자를위한 적절한 답변 형식으로 전체를 쓸 것입니다. 요컨대


:

DataTables sample PHP script 컬럼 별명 사용의 가능성을 허용하지 않거나, MySQL의 기능은 필드리스트 메커니즘 부른다. 이는 JOIN을 수행하거나 일부 형식을 변경하지 않고도 수행 할 수없는 결과에 대한 데이터 변환을 적용하는 모든 쿼리를 만듭니다.

위에서 링크 된 수정 된 버전은 필드 별칭을 안전하게 사용하기 위해 regex를 사용하지만 여전히이 인스턴스에서 원하는 결과를 생성하는 데 필요한 함수 호출을 허용하지 않습니다. 이것은 변환 작업을 PHP로 바꿈으로써 사용자 정의 된 날짜 범위를 반환 된 결과에 적용하기 위해 몇 가지 추가 수정을 통해 해결되었습니다.

정확하게 수행 된 작업에 대한 자세한 내용과 위 링크를 보면 왜 볼 수 있습니까? 나는 미래의 방문자에게 잠재적으로 유용하는 과정에서 떨어져 촬영 한 한 가지가 있다면


, 그것은 이것이다 :

If you need anything more than the very simplest query for use with DataTables, you'd best make damn sure you understand every last line of the back end code and how it interfaces with the front end.

The supplied example scripts do make a good starting point but they are by no means exhaustive in terms of their capabilities, and you need to be prepared to modify or even completely rewrite them in order to achieve your desired result.

+0

이것이 도움이되었습니다 ... –

+0

Dave 분 있나요? 옛 대화방에서 나를 만나요. –

+0

데이브 (Dave) 당신이 저를 도울 수 있고 제가 어떻게이 작업을 수행하여 특정 열의 행에 넣을 수 있는지 아이디어를 제공 할 수 있습니까? 나는 SQL에서 이것을 시도했지만 작동하지만이 스크립트에서는 작동하지 않는다. 당신은 나를 PHP에서 어떻게하는지 또는 정규식을 바꿀 수있는 아이디어를 줄 수 있습니까? (wa_a.DTI/100) * wa_b.INCR_PERCENT' –

관련 문제