Timeout when I execute query from php code - php

I'm using PHP with oracle 10 as database when I execute this query I get always a timeout problem, I've raise it to 1000s but it still the same problem.
I've checked oracle logs but I didn't find any information
DECLARE RET NUMBER;
CHR VARCHAR2(80);
BEGIN
DBMS_PIPE.PURGE('SPAq3qefqefhd1f19b21c3a7gvt30');
DBMS_PIPE.PACK_MESSAGE('q3qefqefhd1f19b21c3a7gvt30;100;14;3345 0047 10/02/2023 S X2009292 ');
RET := DBMS_PIPE.SEND_MESSAGE('SPA');
IF RET = 0 THEN
RET := DBMS_PIPE.RECEIVE_MESSAGE('SPAq3qefqefhd1f19b21c3a7gvt30', '100');
IF RET = 0 THEN
DBMS_PIPE.UNPACK_MESSAGE(CHR);
:ret_string := CHR;
ELSIF RET = 1 THEN
:ret_string := 'KOTIMEOUT';
ELSE :ret_string := 'KOCOMMERROR';
END IF;
ELSIF RET = 1 THEN
:ret_string := 'KOTIMEOUTSEND';
ELSIF RET = 3 THEN
:ret_string := 'KOINTERRUPTSEND';
ELSE
:ret_string := 'KOERRORSEND';
END IF;
END;

Related

MySql stored procedure validation

I need a stored procedure to perform an action according to a condition that is saved in a PHP variable. This stored procedure would run one of possible 2 insert operations:
if $var = 1 then insert 1 else insert 2
Is this possible or will I have to do 2 stored procedures and do the validation in the php script?
Example:
DELIMITER $$
CREATE PROCEDURE example_sp
(in val1 char(100), in val2 char(50), in val3 char(10), in val4 char(50))
BEGIN
IF($var = 1)
THEN
INSERT INTO table1 (val1, val2) VALUES (val1, val2);
ELSE
INSERT INTO table2 (val1, val2) VALUES (val1, val2);
END IF;
END $$
DELIMITER ;
No you need only one stored procedure.
but for this you need dynamic sql
DELIMITER $$
CREATE PROCEDURE example_sp
(IN var int, in val1 char(100), in val2 char(50), in val3 char(10), in val4 char(50))
BEGIN
IF(var = 1)
THEN
SET #sql := CONCAT("INSERT INTO table1 (",val1,",", val2,") VALUES (?,?);");
SET #a := val1;
SET #b := val2;
ELSE
SET #sql := CONCAT("INSERT INTO table2 (",val1,",", val2,") VALUES (?,?);");
SET #a := val1;
SET #b := val2;
END IF;
PREPARE stmt FROM #sql;
EXECUTE stmt USING #a,#b;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
And in PHP
$stmt=$db->prepare("CALL example_sp(?,?,?,?,?)");
$stmt->bind_param('issss',$var,$val1,$val2,$val3,$val4);
$stmt->execute();

Why my sql statement works on command line but not with mysqli_query on PHP?

I have a sql query that works on command line (in my case, running the query using a GUI), but on PHP, through mysqli_query() function, it doesn't work. The error is a duplicate key error.
The bizarre is: the error doesn't exist. The fact that it runs on command line prove that. Also, after further investigation, the records has shown that the unique key was never broken. Though, mysqli_query() throws the unique key error.
My query is:
UPDATE
frequencia
INNER JOIN
(
SELECT
*,
#row_number_aux : =
CASE
WHEN
#atual_aux = disciplina_id
THEN
#row_number_aux + 1
ELSE
1
END
AS "row_number", #atual_aux : = disciplina_id AS "disciplina_atual"
FROM
frequencia
WHERE
frequencia.data > "2019-06-17"
AND frequencia.aluno_id IN
(
8835, 5553, 3488, 7769, 143, 4406, 1472, 698, 8731, 5221, 4240, 4934, 8426, 8107, 5180, 2146, 5589, 4565
)
)
frequencia_aux
ON frequencia.id = frequencia_aux.id
INNER JOIN
(
SELECT
*,
#row_number : =
CASE
WHEN
#atual = disciplina_id
THEN
#row_number + 1
ELSE
1
END
AS "row_number", #atual : = disciplina_id AS "disciplina_atual"
FROM
(
SELECT
disciplina_id,
data,
tempo,
turma_id
FROM
frequencia
WHERE
data > "2019-06-17"
AND turma_id = "87"
GROUP BY
data,
disciplina_id,
tempo
)
frequencia
)
aux
ON frequencia_aux.disciplina_id = aux.disciplina_id
AND frequencia_aux.data = aux.data
AND frequencia_aux.row_number = aux.row_number
SET
frequencia.turma_id = aux.turma_id,
frequencia.tempo = aux.tempo;
I already used the # variables before with no problems, and all type of complex statements, and never got an error like this. Does anyone experienced something like this before?
Solved.
The problem was: the # variables seems to be initialized automatically when running direct on mysql cli, but not through mysqli_query.
One simple join on the subqueries did the job.
Thanks #spencer7593 for the insight.
The final sql:
UPDATE
frequencia
INNER JOIN
(
SELECT
*,
#row_number_aux :=
CASE
WHEN
#atual_aux = disciplina_id
THEN
#row_number_aux + 1
ELSE
1
END
AS "row_number", #atual_aux := disciplina_id AS "disciplina_atual"
FROM
frequencia
join (select #row_number_aux := 0, #atual_aux := 0) xxx
WHERE
frequencia.data > "2019-06-17"
AND frequencia.aluno_id IN
(
8835, 5553, 3488, 7769, 143, 4406, 1472, 698, 8731, 5221, 4240, 4934, 8426, 8107, 5180, 2146, 5589, 4565
)
)
frequencia_aux
ON frequencia.id = frequencia_aux.id
INNER JOIN
(
SELECT
*,
#row_number :=
CASE
WHEN
#atual = disciplina_id
THEN
#row_number + 1
ELSE
1
END
AS "row_number", #atual := disciplina_id AS "disciplina_atual"
FROM
(
SELECT
disciplina_id,
data,
tempo,
turma_id
FROM
frequencia
WHERE
data > "2019-06-17"
AND turma_id = "87"
GROUP BY
data,
disciplina_id,
tempo
)
frequencia
join (select #row_number := 0, #atual := 0) xxx
)
aux
ON frequencia_aux.disciplina_id = aux.disciplina_id
AND frequencia_aux.data = aux.data
AND frequencia_aux.row_number = aux.row_number
SET
frequencia.turma_id = aux.turma_id,
frequencia.tempo = aux.tempo

Query account running balance with php

Please assist, how to write php code for the following query:
SELECT transactions_id, trn_date, trn_dt_total, trn_ct_total, trn_description, trn_bank_reference, balance
FROM
(SELECT t.*, #n := IF(#g <> transactions_id, 0, #n) +
COALESCE(trn_dt_total,0) - COALESCE(trn_ct_total, 0) balance, #g :=
transactions_id
FROM transactions t, (SELECT #n := 0) n, (SELECT #g := 0) g
WHERE trn_building_id = 1 and trn_unit_id = 1 and trn_tenant_id = 1
ORDER BY transactions_id, trn_date)
query
My php page query
$details = $db->query();
When running the query in MySql without the "query" line i get the error:
1248 - Every derived table must have its own alias
The error is self explanatory. You should name an alias for the table you created. After the subquery in the parenthesis, you should write as t_name where tname would be your table alias

MySQL Stored Procedure erro

I am getting this error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lstart, lend; END IF; ELSE IF lstart = -1 THEN select count(*) a' at line 8"
on this stored procedure creation on server but in my localhost phpmyadmin it is creating without any error?
DELIMITER
create procedure getallmedia(IN cat int(2), IN asc_desc varchar(5), IN lstart int(11), IN lend int(11))
BEGIN
IF cat = -1 THEN
IF asc_desc = 'asc' THEN
IF lstart = -1 THEN
select count(*) as result from bc_media where id <> -1;
ELSE
select * from bc_media where id <> -1 limit lstart, lend;
END IF;
ELSE
IF lstart = -1 THEN
select count(*) as result from bc_media where category = cat;
ELSE
select * from bc_media where category = cat order by id desc limit lstart, lend;
END IF;
END IF;
ELSE
IF asc_desc = 'asc' THEN
IF lstart = -1 THEN
select count(*) as result from bc_media where category = cat;
ELSE
select * from bc_media where category = cat limit lstart, lend;
END IF;
ELSE
IF lstart = -1 THEN
select count(*) as result from bc_media where category = cat;
ELSE
select * from bc_media where category = cat order by id desc limit lstart, lend;
END IF;
END IF;
END IF;
END
DELIMITER ;
Help me out please :(
I'm not sure, but, at the first DELIMITER try to put $$
And after the last END put $$

"Did you mean" feature on a dictionary database

I have a ~300.000 row table; which includes technical terms; queried using PHP and MySQL + FULLTEXT indexes. But when I searching a wrong typed term; for example "hyperpext"; naturally giving no results.
I need to "compansate" little writing errors and getting nearest record from database. How I can accomplish such feaure? I know about Levenshtein distance, Soundex and Metaphone algorithms but currently not having a solid idea to implement this to querying against database.
Thanks
See this article for how you might implement Levenshtein distance in a MySQL stored function.
For posterity, the author's suggestion is to do this:
CREATE FUNCTION LEVENSHTEIN (s1 VARCHAR(255), s2 VARCHAR(255))
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETURN s2_len;
ELSEIF s2_len = 0 THEN
RETURN s1_len;
ELSE
WHILE j <= s2_len DO
SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
END WHILE;
WHILE i <= s1_len DO
SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
WHILE j <= s2_len DO
SET c = c + 1;
IF s1_char = SUBSTRING(s2, j, 1) THEN SET cost = 0; ELSE SET cost = 1; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
IF c > c_temp THEN SET c = c_temp; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
IF c > c_temp THEN SET c = c_temp; END IF;
SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
END WHILE;
SET cv1 = cv0, i = i + 1;
END WHILE;
END IF;
RETURN c;
END
He also supplies a LEVENSHTEIN_RATIO helper method which will evaluate the ratio of different/total characters, rather than a straight edit distance. For instance, if it's 60%, then three-fifths of the characters in the source word are different from the destination word.
CREATE FUNCTION LEVENSHTEIN_RATIO (s1 VARCHAR(255), s2 VARCHAR(255))
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, max_len INT;
SET s1_len = LENGTH(s1), s2_len = LENGTH(s2);
IF s1_len > s2_len THEN SET max_len = s1_len; ELSE SET max_len = s2_len; END IF;
RETURN ROUND((1 - LEVENSHTEIN(s1, s2) / max_len) * 100);
END
From the comments of http://dev.mysql.com/doc/refman/5.0/en/udf-compiling.html
now i download the package from the mysql udf repository
http://empyrean.lib.ndsu.nodak.edu/~nem/mysql/
wget http://empyrean.lib.ndsu.nodak.edu/~nem/mysql/udf/dludf.cgi?ckey=28
ll
tar -xzvf dludf.cgi\?ckey\=28
gcc -shared -o libmysqllevenshtein.so mysqllevenshtein.cc -I/usr/include/mysql/
mv libmysqllevenshtein.so /usr/lib
mysql -uroot -pPASS
mysql> use DATABASE
mysql> CREATE FUNCTION levenshtein RETURNS INT SONAME 'libmysqllevenshtein.so';
mysql> select levenshtein(w1.word,w2.word) as dist from word w1, word w2 where ETC........... order by dist asc limit 0,10;
I suggest that you generate typo variations on the query input.
i.e. hyperpext > { hyperpeext, hipertext, ... } etc
One of these is bound to be the correct spelling (especially for common misspellings)
The way you identify the most likely match is to do a lookup for each on an index which tells you the document frequency of the term. (make sense?)
Why not add a table column for storing the word in its alternate (e.g., Soundex) form? that way, if your first SELECT does not find the exact match, you can do a second search to look for matching alternate forms.
The trick is to encode each word so that misspelled variations end up converted into the same alternate form.

Categories