Repeating a mysql query every 5 minutes

Hi guys,

I have an sql query that looks like this:

    <?php
        include("settings.php");

        $sql = conn->query("SELECT * FROM table LIMIT 1");

        $content = $sql->fetch_assoc();

        $id = $content['id'];

        /* PHP Operations here. */
        ?>

Now, I want to be able to repeat this sql query every lets say 5 minutes, because the tables content will change. How can I do this? Is there a way to do an sql request in a javascript or ajax script?

Make it a Cron Job, you should be able to set one up from your Control Panel.

I need to do this without refreshing the page, My hosting has indeed the cron job service, but I don’t see how that will help my problem.

Well you can’t do that with PHP AFAIK. You’ll need to use JavaScript.

1 Like

I know, thats good for me, I would love to do it with javascript. In fact, I need to do it with javascript. But as far as I learnt, you cant run a mysql query inside a javascript function can you?

If you can, please give me a simple exemple.

the query you are running will return only one row, and which row it returns is indeterminate

please, add an ORDER BY clause before the LIMIT

I wanted to return the first row of the table. I thought this was the way.

"SELECT * FROM table ORDER BY id LIMIT 1"

Will set the order.
Though I think it will default to that, if id is the primary key, but at least you can be sure this way.

it worked fine anyways, but yours work too. For me when something does what I want, I always have the idea that its ok to stick with it. But you guys learn me that I should always do the right way, and take precautions.

About doing it every 5 minutes. Can someone show me how can I use ajax or javascript for this. Because on another forum, someone asked the same, and they said it’s not possible to do mysql query in a javascript function… How will I than?

if id is an auto_increment column, then querying with ORDER BY id LIMIT 1 will return the same row every time

so you might as well store its details in php variables, and forget about re-querying

which row do you ~really~ want? and, if i may ask, why?

2 Likes

I actually didn’t provide the exact code and table I use in my file.
Here is the table:

1 column: usernames.

Exemple of the table:

username1
username2
username3
…

In file1.php I read this tables first row.
In another php file, this tables first row changes at some point. It gets deleted.
Than I have the table:

username2
username3

Now, I want in file1.php to reload this data, so that the first row is now username2 and no more username1.

Thats it. I want to do this.

Kind of like an array push / shift ?

I have no idea what that is.

If I have an array 0=>“one”, 1=>“two”, 2=>“three”
I push “four”
the array is now 0=>“one”, 1=>“two”, 2=>“three”, 3=>“four”
I shift
the array is now 0=>“two”, 1=>“three”, 2=>“four”

[quote=“samilkarahisar, post:11, topic:216620, full:true”]
I actually didn’t provide the exact code and table I use in my file.[/quote]

shame on you

[quote=“samilkarahisar, post:11, topic:216620, full:true”]
Now, I want in file1.php to reload this data, so that the first row is now username2 and no more username1.[/quote]

okay

seems legit

You need to keep in mind that not all of your site’s users will have javascript enabled, so for them the “auto refresh” will be broken, the only reliable way is to use a cron job

Yes but I have a page that already uses javascript for a counter. Because all of this is to give a user only 5 minutes for a page. So even tho I do this one with cron job. Than the counter will still need them enabling javascript.

I think I just got another idea. I may achieve all of this without javascript. Thank you for opening my eyes on this point.

Edit: Its amazing how the brain can focus on his first thought and try hard for that, instead of thinking some more to find a better solution. This is why I love programming. It opens my mind to understand that there is never only one solution. In fact there are always better ones than the one we come up with at first. Thanks alot again for you help.

Unless it’s purely for “fun”, a very good idea.

No, the website isn’t for fun, I am hoping people to actually use it. Thats why I will do a system based on the server time.

I don’t understand the gif you sent tho.