无法使用存储过程将数据发送到mysql

时间:2017-06-08 11:20:18

标签: php mysql stored-procedures

美好的一天,

我正在尝试通过调用php函数在mysql服务器上运行存储过程。

我已经使用了很多'echo'行来检查问题的位置但是当我尝试发送数据时,似乎问题发生在我的进程的最后(即'$ idComponent'和'$ data_get')。但是,即使我设置了固定值(id组件为50,值为50),存储过程也不会向我的表发送数据。

在讨厌你之前,我一直在谷歌搜索一段时间,但现在我很失落,因为我现在还不知道在哪里看。

<?php

/*
    php_update_survey_local.php

    *****************************************************************************************
    * This script updates the value of the components in the table tbl_domotique_components
    * It calls the stored procedure 'sp_tbl_eedomus_local_surveys_insert'
    *****************************************************************************************

    Version 1.00, 08.06.2017, Initial version of the script
*/ 

mainProcess();

function mainProcess()
{
    $stringLen = 0;
    $stringValue1 = "";
    $stringValue2 = "";
    $mavalue = "vide";
    $data_get = -9999.999;
    $eedomus_api_link = "https://api.eedomus.com/get?api_user=user&api_secret=data&action=periph.caract&periph_id=";
    $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    echo "Link : "  . $actual_link . "<br>" ;

    $stringLen = strlen($actual_link);
    echo "Length : " . $stringLen;

    $stringPos1 = strpos($actual_link, 'Param1');
    $stringPos2 = strpos($actual_link, '&', $stringPos1);
    $stringValue1 = substr($actual_link, $stringPos1+7, $stringPos2 - $stringPos1 - 7);
    echo "Pos : " . $stringPos2;
    echo "Extracted string 1 : #" . $stringValue1 . "#<br>";

    $stringPos1 = strpos($actual_link, 'Param2', $stringPos2);
    $stringValue2 = substr($actual_link, $stringPos1+7, $stringLen - $stringPos1 - 7);
    echo "Pos : " . $stringPos1;
    echo "Extracted string 2 : #" . $stringValue2 . "#<br>";

    $ServerIP = "myIPAddress";
    $sqlUser = "domoos";
    $sqlDatabase = "domoos_test_db";
    $pw = "toto";
    $file_input_1 = "../auto/file/update_data_" . $stringValue1. ".txt" ;

    $eedomus_component_path_full_1 = $eedomus_api_link . $stringValue1;

    getFile($file_input_1, $eedomus_component_path_full_1);
    $data_get = extractData($file_input_1, $eedomus_component_path_full_1);

    // Connect
    $mysqli = new mysqli($ServerIP, $sqlUser, $pw, $sqlDatabase);
    if(!$mysqli) {
    header('Location: error.php?error=DbConnectionFailure');
    die();
    }

    $idComponent = intval($stringValue1);

    echo "ID COMP = " . $idComponent;
    echo "VALUE = " . $data_get;

    // Prepare IN parameters
    // $mysqli->query("SET @p_Id_Component = '" .  $idComponent . "'");
    // $mysqli->query("SET @p_Value = '" .  $data_get . "'");

    $mysqli->query("SET @p_Id_Component = 50");
    $mysqli->query("SET @p_Value = 50");

    if(!$mysqli->query("CALL sp_tbl_eedomus_local_surveys_insert (@p_Id_Component, @p_Value)"))
    {
        echo "OK";
    if($mysqli) $mysqli->close(); // Close DB connection
    //header('Location: error.php?error=QueryFailure');
    die();
    }

    if($mysqli) $mysqli->close(); // Close DB connection    
}


function getFile($file_input, $eedomus_component_path_full_1)
{
    file_put_contents($file_input, fopen($eedomus_component_path_full_1, 'r'));
    sleep(1);
}

function extractData($file_input, $eedomus_component_path_full_1)
{   
    // Fonction pour mettre à jour la température dans domoos

    if ( 0 == filesize( $file_input ) )
    {
        // file is empty - the treatment is stopped here
        // writeToLog($log_file_name, "[Failure] Empty file detected (" . $file_input . ")");
    }
    else
    {
        echo "fichier PAS vide";
        $consolidated = "";

        $lines = file($file_input);
        // writeToLog($log_file_name, "[Success] Eedomus component data collected (" . $eedomus_component_path_full_1 . " -> " . $file_input . ")");

    foreach($lines as $line)
    {
        $consolidated = $consolidated . $line;
    }

        // Initial values
    $Temperature_Float = 0;
    $Pos1 = 0;
    $Pos2 = 0;
    $Pos3 = 0;
    $Pos4 = 0;


    $Json_Input = $consolidated;


    $Pos1 = strpos($Json_Input, "\"last_value\"", 0);
    $Pos2 = strpos($Json_Input, ":", $Pos1+1);
    $Pos3 = strpos($Json_Input, "\"", $Pos2+1);
    $Pos4 = strpos($Json_Input, "\"", $Pos3+1);
    $Temperature_String = substr($Json_Input, $Pos3+1, $Pos4-$Pos3-1);
    $Temperature_Float = floatval($Temperature_String);

    echo "Coucou. " . "A la maison, il fait maintenant : <b>" . round($Temperature_Float,0) . "&deg;C</b>";
    echo "<br>Contrôle : ";
    echo "<br>Position 1 : " . $Pos1;
    echo "<br>Position 2 : " . $Pos2;
    echo "<br>Position 3 : " . $Pos3;
    echo "<br>Position 4 : " . $Pos4;
    echo "<br>Chaîne extraîte : " . $Temperature_String;

    // return round($Temperature_Float,0);
    return $Temperature_Float;
    }
}
?>

如果它在这里有用的是我的存储过程'sp_tbl_eedomus_local_surveys_insert':

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_tbl_eedomus_local_surveys_insert`(IN `p_Id_Component` INT, IN `p_Value` FLOAT, OUT `p_last_id` INT)
    NO SQL
BEGIN
    INSERT INTO tbl_eedomus_local_surveys   
    (
        eedomus_id,
        eedomus_value
    )
    VALUES
    (
        p_Id_Component, 
        p_Value 
    );
    SET 
    p_last_id = LAST_INSERT_ID();
END$$
DELIMITER ;

知道我在这里做错了什么吗?非常感谢您的时间和阅读我。

0 个答案:

没有答案