jason5137715
jason5137715

Reputation: 117

What I'm doing wrong? "cronjob"

I have MySQL database, it contain a table with a column called "downloads"

I want to update this column to 0 every 24h, but it seems my code doesn't work!

I have folder on the server named cron. Inside it there is two files, one to connect to the database, and the other contain the php code to reset the column downloads to 0

This is my connection code:

   <?php
$link = mysqli_connect("localhost", "test", "root", "test1");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
?>

And my php code that I want to use it in cronjob is this one:

<?php
require_once('/home/cron/connect.php'); // connect to db
$mysqli = ("update users set downloads = 0");
$mysqli->close();
?>

I fired the file directly from the browser but it doesn't reset the column downloads to zero! what I'm doing wrong?

Note: of course there is .htaccess file to protect direct access to the connection file

EDIT: There is no error at connection if I run the code of connection, but the second code from cronjob doesn't work!

Upvotes: 0

Views: 59

Answers (1)

Dharman
Dharman

Reputation: 33395

You do not need $mysqli->close(); at all. Your connection object is called $link. The second line should be:

$link->query("update users set downloads = 0");

You should probably also check if it executed properly and if not do something about it.
Your full code in second file could look like this (assuming the connection is successful):

<?php
require_once('/home/cron/connect.php'); // connect to db
if( $link->query("update users set downloads = 0") ){
    // succesful
} else {
    // fail
}

Upvotes: 2

Related Questions