数据库连接文件无法正常工作

时间:2015-08-29 20:51:48

标签: php mysql

以下文件正常工作(接受来自python的JSON字符串并插入数据。但是,当我将数据库连接信息分解为单独的文件时,没有上传任何内容。当我在浏览器外调用接收器脚本时python脚本我得到"连接成功"以及错误,因为没有传递JSON数据(这是预期的)。

这有效:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "practice";

    $conn = new mysqli($servername, $username, $password, $dbname);
    if( $conn->connect_error ) die("Connection failed: " . $conn->connect_error);
    $payload_dump = $_POST['payload'];

    $payload_array = json_decode($payload_dump,true);

    if( is_array( $payload_array ) ){

        $queries=array();

        foreach( $payload_array as $row ){
            //get the data_payload details
            $device = $row['device'];
            $type = $row['data_type'];
            $zone = $row['zone'];
            $sample = $row['sample'];
            $count = $row['count'];
            $time = $row['date_time'];
            $epoch = $row['epoch_stamp'];

            /*note: we do not need to add the semi-colon here as it gets added later when we implode the array */
            $queries[]="INSERT INTO `data` ( `device`, `type`, `zone`, `sample`, `count`, `date_time`, `epoch_stamp` ) VALUES ('$device', '$type', '$zone', '$sample', '$count', '$time', '$epoch')";
        }
        /*
            Previously the below query was being execute on every iteration
            ~ because $epoch is now the last one encountered in the array, 
            the value that is updated in ALL records is as it would have been
            previously.
        */
        $queries[]="UPDATE `data` SET `date_time` = from_unixtime( $epoch ) WHERE date_time = 0;";

        $sql=implode( ';', $queries );
        if ( $conn->multi_query( $sql ) === TRUE ) {
            echo "New records created and updated successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
    $conn->close();

?>

这不是:

<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password);
if( $conn->connect_error ) die("Connection failed: " . $conn->connect_error);

?>

有了......

<?php
    include 'dbconnect.php';

    $payload_dump = $_POST['payload'];

    $payload_array = json_decode($payload_dump,true);

    if( is_array( $payload_array ) ){

        $queries=array();

        foreach( $payload_array as $row ){
            //get the data_payload details
            $device = $row['device'];
            $type = $row['data_type'];
            $zone = $row['zone'];
            $sample = $row['sample'];
            $count = $row['count'];
            $time = $row['date_time'];
            $epoch = $row['epoch_stamp'];

            //$queries[]="INSERT INTO `data` ( `device`, `type`, `zone`, `sample`, `count`, `date_time`, `epoch_stamp` ) VALUES ('$device', '$type', '$zone', '$sample', '$count', '$time', '$epoch')";
            $queries[]="INSERT INTO `data_test` ( `device`, `type`, `zone`, `sample`, `count`, `date_time`, `epoch_stamp` ) VALUES ('$device', '$type', '$zone', '$sample', '$count', '$time', '$epoch')";

        }

        //$queries[]="UPDATE `data` SET `date_time` = from_unixtime( $epoch ) WHERE date_time = 0;";
        $queries[]="UPDATE `data_test` SET `date_time` = from_unixtime( $epoch ) WHERE date_time = 0;";

        $sql=implode( ';', $queries );
        if ( $conn->multi_query( $sql ) === TRUE ) {
            echo "New records created and updated successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
    $conn->close();

?>

1 个答案:

答案 0 :(得分:2)

看到你可能没有收到评论通知,也许是答案。

根据我的评论,您尚未选择数据库:

$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password);

按照你所做的那样做:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";

$conn = new mysqli($servername, $username, $password, $dbname);
if( $conn->connect_error ) die("Connection failed: " . $conn->connect_error);

另外,请确保连接文件位于执行代码的同一文件夹中。