MySQL event not working (insert statement) - mysql

I am using PHP to execute the SQL query, the event can be created but no random data is inserted into the database. The global event scheduler is set on.
Is there anything wrong with my code?
What I try to do is to insert random data into the database every second once the event is created.
Below are my code :
<?php
$hostname="localhost";
$username="root";
$password='xxx';
$dbname="database";
$conn= mysqli_connect($hostname,$username,$password,$dbname);
if (mysqli_connect_errno($conn)) {
echo "Failed to connect to database: " . mysqli_connect_error();
}
else {
$query="CREATE EVENT XXX
ON SCHEDULE
EVERY 1 SECOND
STARTS '2019-12-28 9:45:20' ON COMPLETION PRESERVE ENABLE
DO
INSERT INTO water(Id,datetime,ph1,ec1,wt1,
ph2,ec2,wt2,
ph3,ec3,wt3,
ph4,ec4,wt4)
VALUES(NOW(),ROUND(FLOOR(21 * RAND()) / 100, 2) + 7.40,
ROUND(FLOOR(101 * RAND()) / 10, 1) + 650,
ROUND(FLOOR(21 * RAND()) / 10, 1) + 23,
ROUND(FLOOR(21 * RAND()) / 100, 2) + 7.40,
ROUND(FLOOR(101 * RAND()) / 10, 1) + 650,
ROUND(FLOOR(21 * RAND()) / 10, 1) + 23,
ROUND(FLOOR(21 * RAND()) / 100, 2) + 7.40,
ROUND(FLOOR(101 * RAND()) / 10, 1) + 650,
ROUND(FLOOR(21 * RAND()) / 10, 1) + 23,
ROUND(FLOOR(21 * RAND()) / 100, 2) + 7.40,
ROUND(FLOOR(101 * RAND()) / 10, 1) + 650,
ROUND(FLOOR(21 * RAND()) / 10, 1) + 23);";
if(mysqli_query($conn, $query)){
echo "successful";
}else{
echo ("error :". mysqli_error($conn));
}
}
mysqli_close($conn);
?>

Related

SSRS Overload resolution failed because no accessible 'IIf' accepts this number of arguments

I wrote a formula using the values of other textboxes in a textbox, but the system does not accept it. The formula is as below
=IIF(ReportItems!Textbox66.Value>=1,IIF(ReportItems!Textbox257.Value>=500,CInt((ReportItems!Textbox66.Value*100-100)/5),IIF(CInt((ReportItems!Textbox66.Value*100-100)/5)>=4,4,CInt((ReportItems!Textbox66.Value*100-100)/5))),IIF(ReportItems!Textbox257.Value<500,IIF(CInt((100-ReportItems!Textbox66.Value*100)/5)>-4,-4,IIF((100-ReportItems!Textbox66.Value*100)/5*(-1))),(CInt((100-ReportItems!Textbox66.Value*100)/5*(-1)))))
It looks like there's an IIF statement with only one of the three arguments.
IIF((100-ReportItems!Textbox66.Value*100)/5*(-1))
It should have an argument for the value when the IIF expression is True of False.
IIF((100-ReportItems!Textbox66.Value * 100) / 5 * (-1), ?, ?)
It's easier to find if you use returns and tabs to break up the expression. A few spaces don't hurt either.
=IIF(ReportItems!Textbox66.Value >= 1,
IIF(ReportItems!Textbox257.Value >= 500,
CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
IIF(CInt((ReportItems!Textbox66.Value * 100 - 100) / 5) >= 4,
4,
CInt((ReportItems!Textbox66.Value * 100 - 100) / 5)
)
),
IIF(ReportItems!Textbox257.Value<500,
IIF(CInt((100 - ReportItems!Textbox66.Value * 100) / 5) > -4,
-4,
IIF((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1), ?, ?)
),
CInt((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1))
)
)
It might be a bit more tedious to use a SWITCH but it may be easier to read and make it work the way you want.
=SWITCH(ReportItems!Textbox66.Value >= 1 AND ReportItems!Textbox257.Value >= 500, CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
ReportItems!Textbox66.Value >= 1 AND CInt((ReportItems!Textbox66.Value * 100 - 100) / 5) >= 4, 4,
ReportItems!Textbox66.Value >= 1, CInt((ReportItems!Textbox66.Value * 100 - 100) / 5),
ReportItems!Textbox257.Value < 500 AND CInt((100 - ReportItems!Textbox66.Value * 100) / 5) > -4,
-4,
ReportItems!Textbox257.Value < 500 AND (100 - ReportItems!Textbox66.Value * 100) / 5 * (-1) = ?????, ?????,
ReportItems!Textbox257.Value < 500, CInt((100 - ReportItems!Textbox66.Value * 100) / 5 * (-1))
)

mysql how to show order number in result

so here is the query
select * from test order by pow(c1 - 8, 2) + pow(c2 - 5, 2) limit 3
is there any way to show order section -> "pow(c1 - 8, 2) + pow(c2 - 5, 2)" for each record in result ?
Just include it in the select:
select t.*, pow(c1 - 8, 2) + pow(c2 - 5, 2) as distance_squared
from test t
order by distance_squared
limit 3

MySQL Calculating the sum of each digit of an Text value

How to calculate the sum of each digit of an Text value in MySQL?
for example
SET #Chars = CONCAT('1','2','33');
-- #Chars = '1233'
-- and result should be 1+2+3+3 = 9.
Yeah, not pretty to do, but here's a way to do it. I'm like you -- had to do it for some spot checking. We do check digit calculations and I needed some specific accounts where the digits would sum in various ways.
select (substring('123456', 1, 1) +
substring('123456', 2, 1) +
substring('123456', 3, 1) +
substring('123456', 4, 1) +
substring('123456', 5, 1) +
substring('123456', 6, 1) +
substring('123456', 7, 1) +
substring('123456', 8, 1) +
substring('123456', 9, 1) +
substring('123456', 10, 1) +
substring('123456', 11, 1) +
substring('123456', 12, 1)) as sumOfDigits;
So just replace '123456' with your string and then make sure that the 1..12 in this example is enough to cover the length of the max string you are expecting to calculate a total for.
Here's my real world example:
select (substring(a.member_number, 1, 1) +
substring(a.member_number, 2, 1) +
substring(a.member_number, 3, 1) +
substring(a.member_number, 4, 1) +
substring(a.member_number, 5, 1) +
substring(a.member_number, 6, 1) +
substring(a.member_number, 7, 1) +
substring(a.member_number, 8, 1) +
substring(a.member_number, 9, 1) +
substring(a.member_number, 10, 1) +
substring(a.member_number, 11, 1) +
substring(a.member_number, 12, 1)) as sumOfDigits,
a.member_number from account a
left join account a2 on a2.member_number = a.member_number and a2.discriminator = 'D'
where a.discriminator = 'S'
and a2.account_id is null having sumOfDigits = 16;
According to https://en.wikipedia.org/wiki/Digital_root there is very simple formula to do it.
In MySQL in can be specified this way:
#Chars - 9 * FLOOR( (#Chars-1 ) / 9 ).
Or any field / expression can be used instead #Chars.

Does mysql query cache the dynamically calculated columns

I have a mysql query:
SELECT my_table.* WHERE SOUNDEX(my_col)='whatever' OR SUBSTR(SOUNDEX(my_col),4)='whatever' ORDER BY SUBSTR(SOUNDEX(my_col),4)='whatever',SOUNDEX(my_col)='whatever'
How many times will the substring function and soundex functions will actually be called? I mean for exactly same inputs will mysql cache the results over the span of one query?
If not, how can I make the change in the query so that each function is called minimum times possible.
MySQL would call this function four times for every returned row, to avoid this you can use a subquery, so instead of
SELECT *
FROM song
ORDER BY Substr(pre_calculated_soundex, 1, 1) =
Substr(Soundex("aaaa"), 1, 1)
+ Substr(pre_calculated_soundex
, 2, 1) =
Substr
(Soundex("aaaa"), 2, 1)
+ Substr(pre_calculated_soundex, 3, 1)
= Substr(Soundex("aaaa"), 3, 1)
+ Substr(pre_calculated_soundex, 4, 1
)
= Substr(Soundex("aaaa"), 4, 1)
You can do
SELECT * from (select *, Soundex("aaaa") as current_soundex from song)
ORDER BY
Substr(pre_calculated_soundex, 1, 1) = Substr(current_soundex , 1, 1)
+ Substr(pre_calculated_soundex, 2, 1) = Substr(current_soundex , 2, 1)
+ Substr(pre_calculated_soundex, 3, 1) = Substr(current_soundex , 3, 1)
+ Substr(pre_calculated_soundex, 4, 1) = Substr(current_soundex , 4, 1)

How to pass from seconds to time

How can I pass from seconds to time on mysql?
the date format of out is hh:mm:ss.ms
Sorry. I need for example 0.98 sec -> 00:00:00.980; With sec_to_tiem return 00:00:01.
thanks.
I have implemeted of this way:
select concat(if(floor(seconds/(60 * 60)) = 0,'00',lpad(floor(seconds/(60 * 60)),2,'0')),
':',
if(floor(seconds/60) = 0,'00',lpad(floor(seconds / 60),2,'0')),
':',
seconds % 60)
but it have to exist other way more efficient
other way:
CONCAT(lpad(floor(seconds / 3600), 2, '0'), ':',
lpad(floor(seconds / 60), 2, '0'), ':',
lpad(floor(seconds % 60), 2, '0'), '.',
lpad(SUBSTRING_INDEX((seconds * 1000) % 1000, '.', 1), 3, '0'))
SELECT MAKETIME (<yourseconds>/(60*24), <yourseconds>/60, <yourseconds>%60)
or with format
SELECT TIME_FORMAT( MAKETIME( <yourseconds>/ ( 60 *24 ) , <yourseconds>/60, <yourseconds>%60 ) , '%T.%f' )