function [out]=mysql_output(hostname,username,password,query,url)
% [out]=mysql_output(hostname,username,password,query,url)
% This function executes a list of MySql commands where some output is
% required (just like a SELECT command).
% The list of MySql commands can include several commands where the last
% one can be a SELECT command (the output of the function).
% INPUT:
% hostname: 'localhost' or the host name string
% username: MySql username
% password: MySql password
% query: the list with MySql commands. Query is a cell.
% url: the URL of the .php file that is used to interface with MySql
% server
% OUTPUT:
% out: a cell with the results.
% Query escape string
L = length(query);
for ii=1:L
q = query{ii};
q = strrep(q, ' ', '_spazio_'); % ASCII code 32
q = strrep(q, '''', '_apice_'); % ASCII code 39
q = strrep(q, '\', '_slash_'); % ASCII code 92
q = strrep(q, '"', '_virgo_'); % ASCII code 34
q = strrep(q, '#', '_canc_'); % ASCII code 35
q = strrep(q, '%', '_perc_'); % ASCII code 37
q = strrep(q, '&', '_and_'); % ASCII code 38
query{ii} = q;
end
% Set POST parameters
opt{1} = 'hostname';
opt{2} = hostname;
opt{3} = 'username';
opt{4} = username;
opt{5} = 'password';
opt{6} = password;
L = length(query);
myquery = query{1};
for ii=2:L
myquery = strcat(myquery,'_pck_',query{ii});
end
opt{7} = 'myquery';
opt{8} = myquery;
% Retry to read the URL
done = 0;
while done==0
try
% Try to read the URL
HTML = urlread(url,'POST',opt);
done = 1;
catch
% If an error has occurred wait for some time before re-trying
disp('Error during URL reading. Retrying...');
done = 0;
pause(0.2);
end
end
HTMLnum = double(uint8(HTML));
pos = find(HTMLnum == 10);
L = length(pos);
cont = 1;
p0 = 1;
for ii=1:L
p1 = pos(ii)-1;
stringa = HTML(p0:p1);
if ~(length(stringa)==1 && double(uint8(stringa))==10)
out{cont} = stringa;
cont = cont+1;
end
p0 = p1+1;
end