SQL Server Prepared Statement表示无效的列数

时间:2015-06-04 16:20:13

标签: php sql-server

所以我正在这个项目上工作,我正在尝试做一个准备好的语句,这样我就可以处理任何奇怪的对象,比如斜线和什么不是原始数据。但是,当我去执行语句时它一遍又一遍地提供相同的错误:

SQLSTATE: 07002
code: 0
message: [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error

它说我的字段数是不正确的,但我检查了所有变量并确保应该存在的所有内容。所以我猜我只是有点困惑。另外要注意这是我第一次使用sqlsrv预处理语句,所以任何帮助都会很棒!

以下是我的代码:

<?php
    $serverName = "localhost"; 
    $connectionInfo = array( "Database"=>"devel", "UID"=>"root", "PWD"=>"");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    $error_message = "";
    $xml = trim(file_get_contents('/file.xml'));
    $xml = new SimpleXMLElement($xml);
    $truck_number;
    $date_time;
    $speed;
    $heading;
    $gps_quality;
    $latitude;
    $longitude;
    $location;
    $fix_type;
    $ignition;
    $gps_odometer;
    $gps_rolling_odometer;
    $performx_odometer;
    $performx_fuel;
    $performx_speed;
    $performx_idle;
    $packet_id;


    sqlsrv_begin_transaction($conn);
    $error_count = 0;
    $q = "INSERT INTO eagle_devel.dbo.people_net (truck_number, date_time, speed, heading, gps_quality, latitude, longitude, location, fix_type, ignition, gps_odometer, gps_rolling_odometer,
                 performx_odometer, performx_fuel, performx_speed, performx_idle, packet_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";       
    $stmt = sqlsrv_prepare($conn, $q, array(&$truck_number, &$date_time, &$speed, &$heading, &$gps_quality, &$latitude, &$longitude, &$location, &$fix_type, &$ignition, &$gps_odometer, &$gps_rolling_odometer, &$performx_odometer, &$performx_fuel, &$performx_speed, &$performx_idle, &$packet_id));
    foreach($xml->loc_history as $loc_history){
            $truck_number = $loc_history->truck_number;
            $date_time = $loc_history->datetime;
            $speed = $loc_history->speed;
            $heading = $loc_history->heading;
            $gps_quality = $loc_history->gps_quality;
            $latitude = $loc_history->latitude;
            $longitude = $loc_history->longitude;
            $location = $loc_history->location;
            $fix_type = $loc_history->fix_type;
            $ignition = $loc_history->ignition;
            $gps_odometer = $loc_history->gps_odometer;
            $gps_rolling_odometer = $loc_history->gps_rolling_odometer;
            $performx_odometer = $loc_history->performx_odometer;
            $performx_fuel = $loc_history->performx_fuel;
            $performx_speed = $loc_history->performx_speed;
            $performx_idle = $loc_history->performx_idle;
            $packet_id = $xml->packet_id;   


            if(sqlsrv_execute($stmt) === false){
                $error_count++;
                if(($errors = sqlsrv_errors()) != null){
                    foreach($errors as $error){
                        $error_message .= "SQLSTATE: ".$error['SQLSTATE']."<br>";
                        $error_message .= "code: ".$error['code']."<br>";
                        $error_message .= "message: ".$error['message']."<br><br>";
                    }
                }
            }
    }
    if($error_count)
        sqlsrv_rollback($conn);
    else
        sqlsrv_commit($conn);

    print $error_message;
?>

更新:我被告知尝试手动初始化变量,所以我做了,我得到了。

SQLSTATE: IMSSP
code: -16
message: An invalid PHP type for parameter 1 was specified.

我不知道这意味着什么,所以我正在研究。

0 个答案:

没有答案