简体   繁体   中英

Running PHP scripts directly with php.exe

I'm trying to run a PHP script through PHP.exe. The script connects to a MSSQL server and inserts some data, that I should be able to see from a php page through the browser. The issue is, sometimes the script runs fine and data is created, sometimes nothing happens.

This is the script file:

<?php
    include('default_code.php');
    $insquery = "INSERT INTO Messaggi (Object, Text, user_ID, isRead, Time) "
            . "VALUES ('Title', 'Text', 5, 'FALSE', CONVERT(datetime, GETDATE(), 120))";
    $ins = sqlsrv_query($conn, $insquery)
            or die(print_r(array(sqlsrv_errors())));
?>

This is the default_code.php file, containing the connection string for the database:

<?php
    $conn = sqlsrv_connect( '(localdb)\v11.0', array('AttachDBFileName'=>'C:\SIAtest\Database\SIAtest.mdf','Database'=>'SIAtest'))
        or die ("Connection problem: " . print_r(array(sqlsrv_errors())));
    date_default_timezone_set('Europe/Rome');
?>

It should be a fairly simple task, I just can't understand why the script does not always properly run.

try to change your code for next script file

    <?php
    include('default_code.php');
    $insquery = "INSERT INTO Messaggi (Object, Text, user_ID, isRead, Time) "
            . "VALUES ('Title', 'Text', 5, 'FALSE', CONVERT(datetime, GETDATE(), 120))";
    $ins = sqlsrv_query($conn, $insquery);
    if ($ins === false){
            die(print_r(array(sqlsrv_errors())));
    }
?>

default_code.php

    <?php
    $conn = sqlsrv_connect( '(localdb)\v11.0', array('AttachDBFileName'=>'C:\SIAtest\Database\SIAtest.mdf','Database'=>'SIAtest'));
    if ($conn === false){
        die ("Connection problem: " . print_r(array(sqlsrv_errors())));
    }
    date_default_timezone_set('Europe/Rome');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM