user3526204
user3526204

Reputation: 509

php script command line execution issue

I have checked all the questions here and googled all available questions on this. However, nothing helped me debug this.

I have a php script log.php from which I try to run local functions like updating a database and writing to a file log.txt. I am trying to run this with task windows task scheduler from a bat file log.bat placed in the system32 folder. However, this runs fine on the browser and updates the database and the log.txt file, but in the bat file / command line , it executes the php file but neither the database is updated nor the log.txt is updated.

log.php

<?php
    ini_set('display_errors', true);
    error_reporting('E_All');
    $date = date("Y-m-d H:i:s");
    $conn=mysqli_connect('localhost','root','')  or die("CONNECT Error : ".$conn->error);

    $chkuser=mysqli_query($conn,"SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'username')") or die("CHECK USER ERROR : ".$conn->error);

    if($chkuser){
        echo "<p>USER 'username'@'localhost' EXISTS";
    } else {
        $user=mysqli_query($conn,"CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';");
        ///  or die("CREATE USER Error : ".$conn->error);
        if($user) {
            echo "<p>USER 'username'@'localhost' with PASSWORD 'password' CREATED Successfully !</p>";
        } else {
            echo "<p>USER 'username'@'localhost' with  PASSWORD 'password' CREATION FAILED !!!</p>"; 
        }
    }

    $grant = mysqli_query($conn,"GRANT ALL ON db1.* TO 'username'@'localhost'"); 
    //     or die("GRANT PRIVILEGES Error : ".$conn->error);

    if($grant) {
        echo "<p>GRANT ALL PRIVILEGES TO 'avion'@'localhost' EXECUTED Successfully !</p>";
    } else {
        echo "<p>GRANT ALL PRIVILEGES TO 'username'@'localhost'   FAILED !</p>";
    }

    $dbase=mysqli_query($conn," CREATE DATABASE login"); 
    // or die("Table DATABASE Error : ".$conn->error);

    if($dbase) { 
        echo "<p>DATABASE login CREATED Successfully !</p>";
    } else {
        echo "<p> Database Create Error : ".$conn->error."</p>";
    }

    if(mysqli_query($conn,"SHOW DATABASES LIKE 'login'")) {
        mysqli_select_db($conn,'login');
        $table=mysqli_query($conn, "CREATE TABLE login (Date DATETIME, id INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(id))");
        // or die("Table Create Error : ".$conn->error);

        if($table) {
            echo "<p>TABLE login CREATED Successfully !</p>";
        } else {
            echo "<p> Table Create Error :".$conn->error."</p>";
        }

        $addlog=mysqli_query($conn,"INSERT INTO login (Date) VALUES('$date')");

        if($addlog) {
            echo "<p>TABLE log UpDATED !</p>";
        } else {
            echo "<p> Add Log Error :".$conn->error."</p>";
        }

        $msg=$date.PHP_EOL;

        if(is_file("log.txt")) {
            $fh=fopen("log.txt","a");
        } else {
            $fh=fopen("log.txt","w");
        }

        $fw=fwrite($fh, $msg);

        if($fw) {
            echo "<p>Log file Updated !</p>";
        } else {
            echo "<p> Log file Not Updated !</p>";
        }

    } else {
        echo "<p>Table Not Created Bcoz DATABASE login DOES NOT EXIST !</p>";   
    }

    mysqli_close($conn);
?>

log.bat

"C:\Program Files\EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion\php.exe" -f "C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\log\log.php" 

Output in console window when log.bat is shows all success messages. But Database or log.txt is not updated.

What am I doing wrong here ?

PS : I have other two bat files that run well on task scheduler. One sends an email through Gmail and other sends SMS through a third party script.

Upvotes: 1

Views: 203

Answers (1)

Rei
Rei

Reputation: 6363

I decided to test your code and found something odd. But first of all, this line:

error_reporting('E_All');

Is actually the same as this:

error_reporting(0);

Yup, you are disabling error reporting. It should be:

error_reporting(E_ALL);

Now, that odd thing is the log.txt file is actually created in C:\Users\RRadin\AppData\Local\VirtualStore. To remedy this, add __DIR__ to the filename. Like this:

if(is_file(__DIR__."/log.txt")) {
    $fh=fopen(__DIR__."/log.txt","a");
} else {
    $fh=fopen(__DIR__."/log.txt","w");
}

Unfortunately I can't reproduce your database update problem. The login table in my database is updated just fine. I executed the script 6 times and it has 6 records now.

Upvotes: 1

Related Questions