Is it possible to redeclare a MySQL function every time you refresh, this is my code
mysql_query("
DROP FUNCTION IF EXISTS Add_num;
CREATE FUNCTION Add_num(LAT_A INT)
RETURNS INT
READS SQL DATA
DETERMINISTIC
BEGIN
DECLARE Ans BIGINT;
SET Ans = LAT_A + 2;
RETURN Ans;
END");
even though I have the line DROP FUNCTION IF EXISTS Add2;
the function stays the same when refreshing. For example I changed the 2
to 3
in the line SET Ans = LAT_A + 2;
and then refreshed and it still adds 2
not 3
.
I have written solution for you, but here I have used mysqli as you would be aware about mysql_query() doesn't accept semicolon(;) and can execute only one query at a time.
Code is as below :
$mysqli = new mysqli("localhost", "root", "", "test");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!$mysqli->query("DROP FUNCTION IF EXISTS Add_num") ||
!$mysqli->query("CREATE FUNCTION Add_num(LAT_A INT) RETURNS INT READS SQL DATA DETERMINISTIC BEGIN DECLARE Ans BIGINT; SET Ans = LAT_A + 4; RETURN Ans; END;")) {
echo "Function creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$select_qry = " SELECT Add_num(6) as result";
$result = $mysqli->query($select_qry);
if ($result) {
$row = $result->fetch_assoc();
echo "Result is :{$row['result']}";
}
$mysqli->close();