谷歌图表MySQL查询错误

时间:2014-06-11 00:49:32

标签: javascript php mysql google-visualization

当我尝试使用以下查询来填充我的饼图时,我收到以下错误。当我在 phpMyAdmin

中测试时,查询工作正常

错误:

  

解析错误:语法错误,意外''和DAY(scheduledTimeStamp)= D'   第34行的chartEvents.php中的(T_CONSTANT_ENCAPSED_STRING)

查询:

SELECT
    `childevent`,
        (COUNT( NULLIF(TRIM(`cblPackage`), '') ) + COUNT(NULLIF(TRIM(`internetPackage`), '')  ) + COUNT( NULLIF(TRIM(`phonePackage`), '') )) AS `TotalCore` 
        FROM `workorder` 
        WHERE bookedTimeStamp != '' AND DAY(bookedTimeStamp) = DAY(CURDATE()) AND MONTH(bookedTimeStamp) = MONTH(CURDATE()) AND YEAR(bookedTimeStamp) = YEAR(CURDATE()) 
        GROUP BY childevent 
        ORDER BY (COUNT( NULLIF(TRIM(cblPackage), '') ) + COUNT(NULLIF(TRIM(internetPackage), ''))+ COUNT( NULLIF(TRIM(phonePackage), ''))) DESC

代码:

 <?php
/*
Script  : PHP-JSON-MySQLi-GoogleChart
Author  : Enam Hossain
version : 1.0

*/

/*
--------------------------------------------------------------------
Usage:
--------------------------------------------------------------------

Requirements: PHP, Apache and MySQL

Installation:

  --- Create a database by using phpMyAdmin and name it "chart"
  --- Create a table by using phpMyAdmin and name it "googlechart" and make sure table has only two columns as I have used two columns. However, you can use more than 2 columns if you like but you have to change the code a little bit for that
  --- Specify column names as follows: "weekly_task" and "percentage"
  --- Insert some data into the table
  --- For the percentage column only use a number

      ---------------------------------
      example data: Table (googlechart)
      ---------------------------------

      weekly_task     percentage
      -----------     ----------

      Sleep           30
      Watching Movie  10
      job             40
      Exercise        20     


*/

/* Your Database Name */

$DB_NAME = 'dbname';

/* Database Host */
$DB_HOST = 'localhost';

/* Your Database User Name and Passowrd */
$DB_USER = 'user';
$DB_PASS = '*********';





  /* Establish the database connection */
  $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }

   /* select all the weekly tasks from the table googlechart */

  $result = $mysqli->query('SELECT `childevent` FROM `workorder` WHERE bookedTimeStamp != '' AND DAY(bookedTimeStamp) = DAY(CURDATE()) AND MONTH(bookedTimeStamp) = MONTH(CURDATE()) AND YEAR(bookedTimeStamp) = YEAR(CURDATE()) GROUP BY childevent ORDER BY (COUNT( NULLIF(TRIM(cblPackage), '') ) + COUNT(NULLIF(TRIM(internetPackage), ''))+ COUNT( NULLIF(TRIM(phonePackage), ''))) DESC');

  /*
      ---------------------------
      example data: Table (googlechart)
      --------------------------
      Weekly_Task     percentage
      Sleep           30
      Watching Movie  10
      job             40
      Exercise        20       
  */



  $rows = array();
  $table = array();
  $table['cols'] = array(

    // Labels for your chart, these represent the column titles.
    /* 
        note that one column is in "string" format and another one is in "number" format 
        as pie chart only required "numbers" for calculating percentage 
        and string will be used for Slice title
    */

    array('label' => 'Event', 'type' => 'string'),
    array('label' => 'Sales', 'type' => 'number')

);
    /* Extract the information from $result */
    foreach($result as $r) {

      $temp = array();

      // The following line will be used to slice the Pie chart

      $temp[] = array('v' =>  $r['childevent']); 

      // Values of the each slice

      $temp[] = array('v' => (int) $r['TotalCore']); 
      $rows[] = array('c' => $temp);
    }

$table['rows'] = $rows;

// convert data into JSON format
$jsonTable = json_encode($table);
//echo $jsonTable;


?>


<html>
  <head>
    <!--Load the Ajax API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">

    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(<?=$jsonTable?>);
      var options = {
           title: 'Sales By Event',
           width: 450,
          height: 150
        };
      // Instantiate and draw our chart, passing in some options.
      // Do not forget to check your div ID
      var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
    </script>
  </head>

  <body>
    <!--this is the div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

这是一个非常基本的问题,您使用单引号来定义空值并包含SQL代码。将您的封闭引号更改为双打,您应该没问题:

$result = $mysqli->query("SELECT `childevent` FROM `workorder` WHERE bookedTimeStamp != '' AND DAY(bookedTimeStamp) = DAY(CURDATE()) AND MONTH(bookedTimeStamp) = MONTH(CURDATE()) AND YEAR(bookedTimeStamp) = YEAR(CURDATE()) GROUP BY childevent ORDER BY (COUNT( NULLIF(TRIM(cblPackage), '') ) + COUNT(NULLIF(TRIM(internetPackage), ''))+ COUNT( NULLIF(TRIM(phonePackage), ''))) DESC");

或者使用反斜杠转义单引号(其中有很多引号,因此在这种情况下更改SQL封闭引号更快)。