0

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!

jason5137715
  • 117
  • 1
  • 8
  • 2
    For starters, you need to stop using `@` to suppress errors, and you need to check for errors after trying to execute the query. Even more importantly, [stop using `mysql_*`](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1); the `mysql_*` functions are outdated, [deprecated](http://us3.php.net/manual/en/intro.mysql.php), and insecure - they have been removed entirely from modern versions of PHP (version 7.0 and higher). Use [`MySQLi`](http://us3.php.net/manual/en/book.mysqli.php) or [`PDO`](http://us3.php.net/manual/en/intro.pdo.php) instead. – elixenide Apr 14 '19 at 22:03
  • `require_once('https://testwebsite.com/cron/connect.php');` that will run the code via Apache\php you want to include it via the local file path –  Apr 14 '19 at 22:05
  • 1
    Also, why are you calling `mysql_query()` twice? The second time makes no sense; `$query` isn’t even defined! – elixenide Apr 14 '19 at 22:05
  • 1
    I also strongly recommend to upgrade to the latest version of PHP if possible. PHP 5 is not longer supported. – Dharman Apr 14 '19 at 22:15
  • @EdCottrell okay I will check that – jason5137715 Apr 14 '19 at 22:18
  • @tim I think this is the problem, I'm gonna check it again "localy" – jason5137715 Apr 14 '19 at 22:18
  • @dharman I'm using 7.2 PHP, not 5 – jason5137715 Apr 14 '19 at 22:19
  • Why down-vote! What I have done?! – jason5137715 Apr 14 '19 at 22:19
  • Then you should not be able to call any of `mysql_*` functions. That must be your problem. Check your error logs. – Dharman Apr 14 '19 at 22:20
  • 1
    The reason for downvotes are explained in the comments by @EdCottrell. In fact it is difficult to treat this question seriously if you have code which is suppressing errors, has code which is never executed and uses long deprecated functions. – Dharman Apr 14 '19 at 22:20
  • Okay I will edit my code – jason5137715 Apr 14 '19 at 22:24
  • @EdCottrell I update my code like what you have said, kindly check the qustion – jason5137715 Apr 14 '19 at 22:39
  • @Dharman I edit the whole code, kindly check my question again – jason5137715 Apr 14 '19 at 22:40
  • You still have `mysql_query`... and you close the connection as soon as it is opened with `mysqli_close($link);` Why? – Dharman Apr 14 '19 at 22:43
  • ops I forget that one I will check it again – jason5137715 Apr 14 '19 at 22:44
  • Well, I'm a beginner to PHP, that's really obvious :P – jason5137715 Apr 14 '19 at 22:46
  • @Dharman I removed mysqli_close($link); and add it after running MySQL query that's make more sense I guess! Can you check the code again! – jason5137715 Apr 14 '19 at 22:53
  • @jason5137715 What you have now, frankly, makes no sense. You’re not even executing the query; you’re just setting a string. – elixenide Apr 14 '19 at 22:59
  • @EdCottrell well this one beyond me, I tried everything and now it start not making any sense, I'm beginner and all tutorial on internet about php 5, at least I made the connection like what you have said without any errors, but I'm stuck at the right way to run MySQL query – jason5137715 Apr 14 '19 at 23:07

1 Answers1

2

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
}
Dharman
  • 30,962
  • 25
  • 85
  • 135