Reputation: 295
I am trying to develop a small webpage which just has to generate random number and post that generated number to MySQL database. it's a simple script which I got from various user's answer and help over the internet. thanks to them . everything is working right but I have a small issue while executing script, when I open that webpage, it generate number and post to Database, but when I close webpage , it stop to generate number, I tried searching everywhere but didn't get actual solution that I want. following are code I am using . Thanks for your help
<?php
include_once 'database.php';
if ($conn) {
echo "success<br>";
}
$digits = 3;
$result = rand(pow(10, $digits-1), pow(10, $digits)-1);
//this is sum
$num = $result;
$sum=0;
$rem=0;
for ($i =0; $i<=strlen($num);$i++)
{
$rem=$num%10;
$sum = $sum + $rem;
$num=$num/10;
$total = substr($sum, -1);
}
//echo "Sum of digits $result is $sum";
$finres = $result.'-'.$total;
// $result = mysql_real_escape_string($result);
// $total = mysql_real_escape_string($total);
// while( $digits = 3 )
// {
// echo $result;
// sleep(10);
// }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="5" >
<title>Ghanta Bazar</title>
</head>
<body>
<p><?php echo $finres; ?></p>
<?php
$sql = "INSERT INTO ghanta_result (result)
VALUES ($finres)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
</body>
</html>
Upvotes: 0
Views: 133
Reputation: 33813
Per the comment regarding regarding using a scheduled Event
entirely within the database this is how you might do that and remove the need for a separate PHP script.
To turn the event scheduler
ON you can run this query:
SET GLOBAL event_scheduler = ON;
Or, better - within the mysqli.ini or .conf file:
event_scheduler = ON;
Given a very basic table structure for ghanta_result
:
mysql> describe ghanta_result;
+-----------+-------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+-------------------+-------+
| result | varchar(50) | YES | | NULL | |
| timestamp | timestamp | YES | | CURRENT_TIMESTAMP | |
+-----------+-------------+------+-----+-------------------+-------+
2 rows in set (0.01 sec)
And a simple random number generator function:
/*********************************************************
utility function to generate a pseudo random number
between low & high numbers where l=low & h=high
*/
CREATE FUNCTION `fn_mt_rand`(
`l` INT,
`h` INT
)
RETURNS int(11)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
begin
return round( ( rand() * ( h - l ) ) + l );
end
And the PHP string generator as a mysql function:
/**********************************************************
function to replicate the PHP string/number generator
as per the question.
*/
CREATE FUNCTION `fnRandomNumberGenerator`(
`p_digits` INT
)
RETURNS varchar(6) CHARSET latin1
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
begin
declare _digits integer default 0;
declare _result integer default 0;
declare _i integer default 0;
declare _len integer default 0;
declare _low integer default 0;
declare _high integer default 0;
declare _sum integer default 0;
declare _rem integer default 0;
declare _num integer default 0;
declare _total integer default 0;
set _i=0;
set _digits=cast( p_digits as unsigned );
set _low=pow( 10, ( _digits - 1 ) );
set _high=pow( 10, _digits );
set _result=fn_mt_rand( _low - 1, _high );
set _num=_result;
set _len=length( _result );
set _sum=0;
set _rem=0;
set _total=0;
while( _i < _len ) do
set _rem=_num % 10;
set _sum=_sum + _rem;
set _num=_num / 10;
set _total=substr( _sum, -1 );
set _i=_i + 1;
end while;
return concat( _result, '-', _total );
end
Could be combined and used in a new event
that would be run to whatever schedule you specify
/*************************************************
The scheduled event to run every N minutes
or whatever the schedule is specified to be
*/
CREATE EVENT `evtGhantaResult`
ON SCHEDULE
EVERY 1 MINUTE STARTS '2022-11-29 09:15:00'
ON COMPLETION PRESERVE
ENABLE
COMMENT ''
DO begin
insert into `ghanta_result` ( `result` ) values ( fnRandomNumberGenerator( 3 ) );
end
Having run for a few minutes:
mysql> select * from ghanta_result;
+--------+---------------------+
| result | timestamp |
+--------+---------------------+
| 137-2 | 2022-11-29 09:15:00 |
| 848-2 | 2022-11-29 09:16:00 |
| 108-0 | 2022-11-29 09:17:00 |
| 716-5 | 2022-11-29 09:18:00 |
| 804-2 | 2022-11-29 09:19:00 |
+--------+---------------------+
5 rows in set (0.00 sec)
Upvotes: 1