1

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.

2
  • Did you check current working directory? Commented Dec 20, 2016 at 8:24
  • Yes @RayRadin. The directories are fine. log.php and log.txt is in the /path/log/ folder and batch file (log.bat) is in windows/system32/ folder. And I am running the log.bat file on command line from inside the system32 folder Commented Dec 20, 2016 at 8:32

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You nailed it @Ray Radin. It was the DIR that did it. As for the E_ALL, if I don't use the quotes, it throws an error "Use of Undefined Constanr E_ALL assumed 'E_ALL' " in cmd window. And the database is getting updated now. I don't know what i tried in between, it started working. Thanks a ton for the solution to the log.txt update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.