[Solved] php + Highcharts.js + convert from mysql to mysqli causes error [duplicate]


If you are going to convert your code to mysqli_*, then it should be in a prepared statement manner.

Lets re-establish your MySQL connection in db.inc.php:

<?php

  $conn = new mysqli("localhost", "Datenbank-Username", "Datenbank-Passwort", "Datenbank-Name");

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

  /* CHANGE CHARACTER SET TO utf8 */
  if (!$conn->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
    exit();
  } else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
  }

  define("listViewTempPeriod", 24); // Anzeige der Stunden die ausgegeben werden sollen
  define("NUMSENSORS", 2);          // Anzahl der Sensoren deren Werte in der Datenbank stehen

?>

Your file where you store your functions (functions.inc.php):

<?php

  function delLastChar($string="")
  {
    global $conn;
    $t = substr($string, 0, -1);
    return($t);
  }

  function getChartValues($sensorID=0, $timePeriodInHours=24)
  {

    global $conn;

    if($stmt = $conn->prepare("SELECT DATE_FORMAT(datumzeit, '%H') AS STUNDE, sensorwert FROM arduino_sensorwerte WHERE sensorid = ? AND datumzeit >= date_sub(NOW(), interval ? hour) AND datumzeit <= NOW() GROUP BY DATE_FORMAT(datumzeit, '%Y-%m-%d %H') ORDER BY datumzeit DESC")){
      $stmt->bind_param("ii", $sensorID, $timePeriodInHours);
      $stmt->execute();
      $stmt->store_result();
      $n_data = $stmt->num_rows;
      $stmt->bind_result($stunde, $sensorwert);

      $chartValues="";
      $stundenValues="";

      while($stmt->fetch()){

        $charValues    = $sensorwert;
        $stundenValues = $stunde;

      } /* END OF WHILE LOOP */
      $stmt->close();
    } /* END OF PREPARED STATEMENT */

    $chartValues   = delLastChar($chartValues);     // Komma hinter dem letzten Temperaturwert entfernen
    $stundenValues = delLastChar($stundenValues);   // Komma hinter letzter Stunde entfernen

    return array($chartValues, $stundenValues);

  } /* END OF getChartValues FUNCTION */

  function getSensorSettings($sensorID=0)
  {

    global $conn;

    $stmt = $conn->prepare("SELECT mpcharttype, mplinetype, mpname, mpdescription, mplinecolor FROM arduino_messpunkte WHERE messpunktid = ?");
    $stmt->bind_param("i", $sensorID);
    $stmt->execute();
    $stmt->store_result();
    $n_data = $stmt->num_rows;
    $stmt->bind_result($mpcharttype, $mplinetype, $mpName, $mpDescription, $mpLineColor);

    if($n_data > 0)
    {
      while($stmt->fetch()){

        switch ($mpcharttype) 
        {
          case 1:
            $mpChartType="spline";
            break;
          case 2:
            $mpChartType="line";
            break;
          case 3:
            $mpChartType="areaspline";
            break;
          case 4:
            $mpChartType="area";
            break;
          case 5:
            $mpChartType="column";
            break;    
          case 6:
            $mpChartType="bar";
            break;    
          } /* END OF FIRST SWITCH */

        switch ($mplinetype) 
        {
          case 1:
            $mpLineType="solid";
            break;
          case 2:
            $mpLineType="ShortDash";
            break;
          case 3:
            $mpLineType="ShortDot";
            break;
          case 4:
            $mpLineType="ShortDashDot";
            break;
          case 5:
            $mpLineType="ShortDashDotDot";
            break;    
          case 6:
            $mpLineType="Dot";
            break;
          case 7:
            $mpLineType="Dash";
            break;
          case 8:
            $mpLineType="LongDash";
            break;
          case 9:
            $mpLineType="DashDot";
            break;
          case 10:
            $mpLineType="LongDashDot";
            break;
          case 11:
            $mpLineType="LongDashDotDot";
            break;              
        } /* END OF SECOND SWITCH */

      } /* END OF WHILE LOOP */

      return array($mpChartType, $mpLineType, $mpName, $mpDescription, $mpLineColor);

    } /* END OF CHECKING NUMBER OF RESULTS */

  } /* END OF getSensorSettings FUNCTION */

?>

If you expect a query to have one row of result only, you don’t need to loop it anymore.

23

solved php + Highcharts.js + convert from mysql to mysqli causes error [duplicate]