Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cron作业中的PHP代码工作不正常_Php_Mysql_Cron_Cron Task - Fatal编程技术网

cron作业中的PHP代码工作不正常

cron作业中的PHP代码工作不正常,php,mysql,cron,cron-task,Php,Mysql,Cron,Cron Task,我有一个PHP脚本,我在其中执行两个步骤 1) 从数据库中删除所有表 2) 正在还原该空数据库中的默认数据库 当我通过放置URL手动运行脚本时,此代码工作正常,但每当我使用cron job运行此脚本时,第一步工作正常,但数据库未恢复 这是我的cron作业命令 [php_path] -q /home/[username]/[php_file_path] 这是我的php脚本 // Name of the file $filename = 'sample.sql'; // MySQL host $m

我有一个PHP脚本,我在其中执行两个步骤

1) 从数据库中删除所有表
2) 正在还原该空数据库中的默认数据库

当我通过放置URL手动运行脚本时,此代码工作正常,但每当我使用cron job运行此脚本时,第一步工作正常,但数据库未恢复

这是我的cron作业命令

[php_path] -q /home/[username]/[php_file_path]
这是我的php脚本

// Name of the file
$filename = 'sample.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'my_username';
// MySQL password
$mysql_password = 'my_password';
// Database name
$mysql_database = 'my_db_name';


/*----------  Drop All tables From Database  ----------*/



$mysqli = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);
$mysqli->query('SET foreign_key_checks = 0');
if ($result = $mysqli->query("SHOW TABLES"))
{
    while($row = $result->fetch_array(MYSQLI_NUM))
    {
        $mysqli->query('DROP TABLE IF EXISTS '.$row[0]);
    }
}

$mysqli->query('SET foreign_key_checks = 1');
$mysqli->close();



/*----------  Restore Database From SQL File  ----------*/



$con=mysqli_connect($mysql_host, $mysql_username, $mysql_password, $mysql_database);
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// ...some PHP code for database "my_db"...

// Change database to "test"
mysqli_select_db($con, $mysql_database);

// ...some PHP code for database "test"...
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
    continue;

// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
    // Perform the query
    mysqli_query($con, $templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
    // Reset temp variable to empty
    $templine = '';
}
}
//文件名
$filename='sample.sql';
//MySQL主机
$mysql_host='localhost';
//MySQL用户名
$mysql_username='my_username';
//MySQL密码
$mysql_password='my_password';
//数据库名称
$mysql_database='my_db_name';
/*----------从数据库中删除所有表----------*/
$mysqli=newmysqli($mysql\u主机,$mysql\u用户名,$mysql\u密码,$mysql\u数据库);
$mysqli->query('SET foreign_key_checks=0');
如果($result=$mysqli->query(“显示表”))
{
而($row=$result->fetch_数组(MYSQLI_NUM))
{
$mysqli->query('DROP TABLE IF EXISTS'。$row[0]);
}
}
$mysqli->query('SET foreign_key_checks=1');
$mysqli->close();
/*----------从SQL文件还原数据库----------*/
$con=mysqli\u connect($mysql\u主机,$mysql\u用户名,$mysql\u密码,$mysql\u数据库);
//检查连接
if(mysqli\u connect\u errno())
{
echo“未能连接到MySQL:”.mysqli_connect_error();
}
//…数据库“my_db”的一些PHP代码。。。
//将数据库更改为“测试”
mysqli_选择_数据库($con,$mysql_数据库);
//…一些用于数据库“测试”的PHP代码。。。
//临时变量,用于存储当前查询
$templine='';
//读取整个文件
$lines=文件($filename);
//每行循环一次
foreach($line作为$line)
{
//如果是评论,请跳过它
if(substr($line,0,2)='--'| |$line='')
继续;
//将此行添加到当前线段
$templine.=$line;
//如果它的末尾有一个分号,那么它就是查询的结尾
if(substr(修剪($line),-1,1)=';')
{
//执行查询
mysqli\u查询($con,$templine)或打印('Error performing query\'。$templine.\':'.mysql\u Error()。

); //将温度变量重置为空 $templine=''; } }
cron在哪个用户下运行,该用户是否有权访问该文件?这一行
$lines=file($filename)之后,我没有得到正确的答案
test
$lines
不是false,比如
if($lines==false){echo“无法读取$filename”;}
在运行此行
mysqli\u select\u db($con,$mysql\u database)之前,还要检查您是否更改了
$mysql\u database
的值cron在哪个用户下运行,该用户是否有权访问文件.means?这一行
$lines=file($filename)之后,我没有得到正确的答案
test
$lines
不是false,比如
if($lines==false){echo“无法读取$filename”;}
在运行此行
mysqli\u select\u db($con,$mysql\u database)之前,还要检查您是否更改了
$mysql\u database
的值