I'm running a connection with PDO to a local MSSQL database. Running any Stored procedure at all through the connection doesn't give me any error at all.
This one single Stored procedure is giving me the following error:
Error in SQL: [Microsoft][SQL Server Native Client 10.0]Invalid cursor state - Query: exec sp_Get_SaldosWeb #Tipo=1, #IdDato=15368
This is my current PDO string for connecting inside this function and returning the array:
$query = $this->db->prepare($qry, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$final = $query->execute();
$this->setRows($query);
if(!$final) {
$this->error($qry, $query, $ret);
} else {
return $query->fetchAll();
}
I've tried closing the cursor both before the execute and after fetchAll like so:
$rows = $query->fetchAll();
$query->closeCursor();
return $rows;
But that also doesn't work. FYI, there are no queries executed before this call to this stored procedure anywhere in my code.
Not sure why this stored procedure is giving so many errors. If I run this exact same SP from MSSQL Management console it runs fine and returns 3 rows.
EDIT:
Here is the stored procedure:
USE [DBNAME]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_Get_SaldosWeb]
#Tipo int , --1 = Alumno 2 = Familia
#IdDato int
as
if #Tipo = 1
begin
select SC_Alumno.Codigo ,
SC_Alumno.Nombres + ' ' + SC_Alumno.Apellidos as [Nombre],
SC_Moneda.Nombre as Moneda, upper(replace(replace(replace(replace(replace( replace(lower(SC_TipoCargo.Nombre),char(237),'i'), char(243),'o'), char(233),'e') , char(225),'a') ,char(250),'u'),char(241),'ñ')) as [Tipo de Cargo]
, cast(round(Sum(SC_CargoxAlumno.Debe),2) as decimal(18,2)) as Monto ,
SC_Alumno.Codigo as Codigo2
from SC_CargoxAlumno
inner join SC_Moneda on SC_CargoxAlumno.IdSC_Moneda = SC_Moneda.IdSC_Moneda
inner join SC_TipoCargo on SC_CargoxAlumno.IdSC_TipoCargo = SC_TipoCargo.IdSC_TipoCargo
inner join SC_Alumno on SC_Alumno.IdSC_Alumno = SC_CargoxAlumno.IdSC_Alumno
inner join SC_Familia on SC_Alumno.IdSC_Familia = SC_Familia.IdSC_Familia
where
SC_Alumno.IdSC_Alumno = #IdDato
and SC_CargoxAlumno.Debe <> 0
group by
SC_Alumno.Codigo ,
SC_Alumno.Nombres + ' ' + SC_Alumno.Apellidos ,
SC_Moneda.Nombre , SC_TipoCargo.Nombre
end
else
begin
select SC_Alumno.Codigo ,
SC_Alumno.Nombres + ' ' + SC_Alumno.Apellidos as [Nombre],
SC_Moneda.Nombre as Moneda, upper(replace(replace(replace(replace(replace( replace(lower(SC_TipoCargo.Nombre),char(237),'i'), char(243),'o'), char(233),'e') , char(225),'a') ,char(250),'u'),char(241),'ñ')) as [Tipo de Cargo] ,
cast(round(Sum(SC_CargoxAlumno.Debe),2) as decimal(18,2)) as Monto ,
SC_Alumno.Codigo as Codigo2
from SC_CargoxAlumno
inner join SC_Moneda on SC_CargoxAlumno.IdSC_Moneda = SC_Moneda.IdSC_Moneda
inner join SC_TipoCargo on SC_CargoxAlumno.IdSC_TipoCargo = SC_TipoCargo.IdSC_TipoCargo
inner join SC_Alumno on SC_Alumno.IdSC_Alumno = SC_CargoxAlumno.IdSC_Alumno
inner join SC_Familia on SC_Alumno.IdSC_Familia = SC_Familia.IdSC_Familia
where
SC_Familia.IdSC_Familia = #IdDato
and SC_CargoxAlumno.Debe <> 0
group by
SC_Alumno.Codigo ,
SC_Alumno.Nombres + ' ' + SC_Alumno.Apellidos ,
SC_Moneda.Nombre , SC_TipoCargo.Nombre
end
Add "SET NOCOUNT ON" to the beginning of your procedure.
You might find references here :
PHP Data Objects
My stored procedure "best practices" checklist
Related
sql query is as follows:
CREATE PROCEDURE Get_concat_tavel_info
AS
SELECT travelDate,empID,
CONCAT(tripStart, ' >> ', tripEnd) AS trips_Code,
CONCAT(startAddress, ' >> ', endAddress) AS trips
INTO #temp
FROM te_tripDetails
ORDER BY travelDate;
SELECT travelDate,empID,
STUFF((SELECT N' ; ' + trips_Code FROM #temp WHERE travelDate = t.travelDate AND empID = t.empID FOR XML PATH, TYPE).value(N'.[1]' , N'nvarchar(max)'),1,1,N'') AS trips_Code,
STUFF((SELECT N' ; ' + trips FROM #temp WHERE travelDate = t.travelDate AND empID = t.empID FOR XML PATH, TYPE).value(N'.[1]' , N'nvarchar(max)'),1,1,N'') AS trips
FROM #temp AS t
WHERE empID = 6 AND travelDate = '2020-05-01'
GROUP BY travelDate,empID
ORDER BY travelDate;
I add it to my PHP/Codeiginiter code to call it as such and this happens:
PHP Code
It seems to not recognize #used here is not for commenting
HELP PLEASE !!!
So, I built this query in SQLYog, and it returned the results I was looking for. However, when I copy-pasted it into php and used mysqli to run each query / fetch the results, my results were different (namely, one field was null rather than the correct results).
Query:
SET SESSION group_concat_max_len = 1000000;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(CASE WHEN field_number = ''',
wp.field_number,
'''
THEN ',
IF(lng.value = NULL, 'wp.value', 'lng.value'),
' ELSE NULL END) AS ',
CONCAT(fm.field_name, IF(fm.rank <> 0, fm.rank, ''))
)
)INTO #sql
FROM wp_rg_lead_detail wp
JOIN vh_rg_form_map fm
ON fm.field_number = wp.field_number
AND fm.form_id = wp.form_id
LEFT JOIN wp_rg_lead_detail_long lng
ON wp.id = lng.lead_detail_id
WHERE wp.form_id = 1;
SET #sql = CONCAT('SELECT lead_id,', #sql, ' FROM wp_rg_lead_detail wp
LEFT JOIN wp_rg_lead_detail_long lng
ON wp.id = lng.lead_detail_id
WHERE form_id = 1 GROUP BY lead_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
My results are almost exactly the same, the only difference lies in the picture field. Here are some pictures of the difference.
Silly mistake on my part: I had to add in a section to the where clause to ensure that I was getting a single lead_id (where lead_id = $n). I'm still unsure as to exactly why I was getting different responses for the same query in php and yog, but fixing my query fixed the problem.
This is my native query:
$query = <<<Query
SET #num = 0;
SET #type = 0;
SELECT md.*, od.id AS order_detail_id, od.erc AS od_ec,
#num := if(#type = od.id, #num + 1, 1) AS row_number,
#type := od.id AS dummy
FROM bar md
LEFT JOIN foobar mb ON md.mbi = mb.id
LEFT JOIN barfoo od ON od.mbid = mb.id
WHERE od.id IN ($where)
AND (od.status = $statusQueued OR od.status = $statusProcessing)
GROUP BY md.id
HAVING row_number <= od.erc
ORDER BY md.qt ASC
LIMIT 0, 1000
Query;
$nativeQuery = $this->_em->createNativeQuery($query, $rsm);
When I execute it, PDO gets upset and throws me an error:
[PDOException] SQLSTATE[HY000]: General error
No more informations.
However when I get rid of first two lines (with "SET") the query runs (however returns wrong results, as the variables must be initialised in this case). How can I force PDO to run this correctly? (when run via PMA it works just fine)
It seems that most elegant solution is to leverage MySQL alternative initialisation syntax:
$query = <<<Query
SELECT md.*, od.id AS order_detail_id, od.erc AS od_ec,
#num := if(#type = od.id, #num + 1, 1) AS row_number,
#type := od.id AS dummy
FROM (SELECT #num := 0, #type := 0) init, bar md
LEFT JOIN foobar mb ON md.mbi = mb.id
LEFT JOIN barfoo od ON od.mbid = mb.id
WHERE od.id IN ($where)
AND (od.status = $statusQueued OR od.status = $statusProcessing)
GROUP BY md.id
HAVING row_number <= od.erc
ORDER BY md.qt ASC
LIMIT 0, 1000
Query;
I ran into the same kind of issue trying to run several queries in one call (in my case it was about using SQL_CALC_FOUND_ROWS and its buddy FOUND_ROWS()).
Doctrine can't handle this. I don't know the official reason, but in my opinion it's probably a security concern (as the EM's calls are then open to basic query injection).
Still in my opinion, the most elegant way of dealing with this is using the EM's transactional mode:
//fetch PDO-ish adapter
$conn = $em->getConnection();
//open an atomic sequence
$conn->beginTransaction();
//do your stuff
$conn->executeQuery('SET #...');
$stmt = $conn->executeQuery('SELECT stuff using #vars');
//commit the atomic sequence
$conn->commit();
//fetch the result, could be useful ;)
$data = $conn->fetchAll();
Hope this helps! (and works in your case...)
I'm here again to ask help again, I'm already created a store procedure in mysql, when a call it on a sgb like SQLyog or MySQL Workbench, I retrieve a result of my query normally, but when I do the same thing on the php and execute mysql_query, nothing is given.
This is my function to execute a mysql query.
function conect($SERVER){
if (strtoupper($SERVER) =='MYSQL') {
$this->con=mysql_connect(SQL_SERVER,SQL_USER,SQL_PASSWD)or die(mysql_error());
$this->db=mysql_select_db(SQL_DB,$this->con);
if ($this->con == 0){
$retorno = "CONECTION ERROR - SERVER!<br>";
}
else if ($this->db == 0){
$retorno = "CONECTION ERROR - DATA BASE!<br>";
} else {
$retorno = "";
}
return $retorno;
}
}
function isConected(){
if($this->con == false)
return false;
return true;
}
function execute($qry, $res="load"){
if(!$this->isConected())
$this->conect('MYSQL');
$this->result[$res] = mysql_query($qry, $this->con) or trigger_error(mysql_error() . ": \n" . __FILE__ . ": \n" . $res . " - " . $qry);
if($this->result[$res])return true;
trigger_error(mysql_error());
return false;
}
function get_fetch_assoc($res="load"){
return mysql_fetch_assoc($this->result[$res]);
}
And this is my procedure call
$mysql = new conexao();
$qry = "CALL spGetChamadoMaisAntigo('".$uareas."', '".$_SESSION["dpto_codigoUrl"]."')";
$mysql->execute($qry, 'Load');
while($row = $mysql->get_fetch_assoc('Load')){
$chamadosAbilitados[] = $row;
}
I have tested this function doing a simple query using a
select * from tableName
and it returned a result normally.
Here is my procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `ocomon_rc6`.`spGetChamadoMaisAntigo`$$
CREATE DEFINER=`renancr`#`%` PROCEDURE `spGetChamadoMaisAntigo`(IN Sistema VARCHAR(1000),IN CodigoUrl INT)
BEGIN
CALL lib_Explode( ',' , Sistema );
DROP TABLE IF EXISTS ocomon_rc6.TempChamados;
CREATE TABLE IF NOT EXISTS ocomon_rc6.TempChamados(numero INT, dias_apos_abertura INT);
INSERT INTO TempChamados(numero, dias_apos_abertura)
SELECT
o.numero,
(CASE
WHEN CONVERT(NOW(), TIME) > CONVERT(o.data_abertura, TIME) THEN DATEDIFF(NOW(), o.data_abertura)
ELSE DATEDIFF(DATE_SUB(NOW(),INTERVAL 1 DAY), o.data_abertura)
END)
FROM
ocorrencias as o
LEFT JOIN
sistemas AS a ON
a.sis_id = o.sistema
LEFT JOIN
sistemas_x_url AS su ON
a.sis_id = su.sis_id
LEFT JOIN
urls ON
su.codigoUrl = urls.codigoUrl
LEFT JOIN
localizacao AS l ON
l.loc_id = o.local
LEFT JOIN
instituicao AS i ON
i.inst_cod = o.instituicao
LEFT JOIN
usuarios AS u ON
u.user_id = o.operador
LEFT JOIN
usuarios AS ua ON
ua.user_id = o.aberto_por
LEFT JOIN
`status` AS s ON
s.stat_id = o.status
LEFT JOIN
status_categ AS stc ON
stc.stc_cod = s.stat_cat
LEFT JOIN
problemas AS p ON
p.prob_id = o.problema
LEFT JOIN
sla_solucao AS sls ON
sls.slas_cod = p.prob_sla
LEFT JOIN
prioridades AS pr ON
pr.prior_cod = l.loc_prior
LEFT JOIN
sla_solucao AS slr ON
slr.slas_cod = pr.prior_sla
LEFT JOIN
script_solution AS sol ON
sol.script_cod = o.oco_script_sol
LEFT JOIN
prior_atend AS prioridade_atendimento ON
prioridade_atendimento.pr_cod = o.oco_prior
LEFT JOIN
sistemas_x_filtro AS filtr ON
filtr.codigoSistemaFiltro = o.filtro
WHERE
s.stat_painel IN (2)
AND o.sistema IN (SELECT val FROM lib_Explode)
AND su.codigoUrl = CodigoUrl
AND o.oco_scheduled = 0
ORDER BY
o.data_abertura;
DROP TABLE IF EXISTS lib_Explode;
SELECT * FROM TempChamados WHERE dias_apos_abertura = (SELECT MAX(dias_apos_abertura) FROM TempChamados);
DROP TABLE IF EXISTS ocomon_rc6.TempChamados;
END$$
DELIMITER ;
Someone have an idea what I'm doing so wrong?
Solved, I have analyzed this tutorial http://www.joeyrivera.com/2009/using-mysql-stored-procedures-with-php-mysqlmysqlipdo/ and it gives me a answer, need to declare all param it need send out to the php, and insert into this parameters the result of select, I have modifyed my procedure, and called the procedure with this output parameters with # and finaly did a simple select of this output parameters with #, like below.
Altered the parameters of the procedure
DROP PROCEDURE IF EXISTS `ocomon_rc6`.`spGetChamadoMaisAntigo`$$
CREATE DEFINER=`renancr`#`%` PROCEDURE `spGetChamadoMaisAntigo`(IN Sistema VARCHAR(1000),IN CodigoUrl INT, OUT numero INT, OUT diasAposAbertura INT)
Altered the internar select and insert it into the out parameters
SELECT idOcorrencia, dias_apos_abertura INTO numero , diasAposAbertura FROM TempChamados WHERE dias_apos_abertura = (SELECT MAX(dias_apos_abertura) FROM TempChamados);
In the php I changed the call of procedure like below.
$qry = "CALL spGetChamadoMaisAntigo('".$uareas."', '".$_SESSION["dpto_codigoUrl"]."', #numero , #diasAposAbertura)";
$mysql->execute($qry, 'Load');
And finally I did a select of this output parameters like below.
$qry = "SELECT #numero , #diasAposAbertura";
$mysql->execute($qry, 'Load');
The following MySQL query runs in PHP without errors, but the resultset is empty. Directly outputting the query string to a file and running the query in the MySQL client using 'source [filename]' returns several rows of results, as expected.
Is there something that would cause this query not to work with PHP? categorylinks.cl_to and smw_spec2.value_string are both varbinary(255). Show create table indicates engine=InnoDB and default charset=binary.
Things I have tried without success:
$sql = preg_replace("/[\n\t]+/", " ", $sql);
Changing '_wpg' and 'Derp' to CAST('_wpg' AS BINARY(255))
Changing '_wpg' and 'Derp' to BINARY '_wpg'
I am using the MediaWiki DatabaseMysql class to execute the query and fetch rows, but it's a very thin abstraction, and I'm certain it's not the problem (see below).
SELECT
prop.name AS prop_name, prop.count AS prop_count, prop.type AS prop_type,
val.value AS val_value, val.unit AS val_unit, val.count AS val_count
FROM
(
SELECT
s_id, name, type, COUNT(foo.name) AS count
FROM (
(
SELECT
cl.cl_to AS cat_name, s.smw_id AS s_id, s.smw_sortkey AS name, spec.value_string AS type
FROM `smw_ids` s
INNER JOIN (`categorylinks` cl, `page` p, `smw_ids` s2, `smw_atts2` a)
ON (cl.cl_from = p.page_id AND
p.page_title = s2.smw_title AND
s2.smw_id = a.s_id AND
a.p_id = s.smw_id)
LEFT JOIN `smw_spec2` spec ON s.smw_id = spec.s_id
)
UNION ALL
(
SELECT
cl.cl_to AS cat_name, s.smw_id AS s_id, s.smw_sortkey AS name, '_wpg' AS type
FROM `smw_ids` s
INNER JOIN (`categorylinks` cl, `page` p, `smw_ids` s2, `smw_rels2` a)
ON (cl.cl_from = p.page_id AND
p.page_title = s2.smw_title AND
s2.smw_id = a.s_id AND
a.p_id = s.smw_id)
)
) AS foo
WHERE foo.cat_name = 'Derp'
GROUP BY name
ORDER BY count DESC
LIMIT 10
) AS prop
INNER JOIN
(
SELECT
bar.p_id AS p_id, bar.value AS value, bar.unit AS unit, COUNT(bar.value) AS count,
IF( #prev != p_id, #rownum := 1, #rownum := #rownum+1 ) AS rank,
#prev := p_id
FROM (
(SELECT a.p_id AS p_id, a.value_xsd AS value, a.value_unit AS unit FROM `smw_atts2` a)
UNION ALL
(SELECT r.p_id AS p_id, s.smw_sortkey AS value, NULL AS unit
FROM `smw_rels2` r INNER JOIN `smw_ids` s ON r.o_id = s.smw_id)
) AS bar
GROUP BY value, unit
ORDER BY count DESC
) AS val
ON prop.s_id = val.p_id
WHERE val.rank <= 50
ORDER BY prop_count DESC, prop_name, val_count DESC, val_value
Edit: The following test script outputs nothing. query.sql contains exactly the query above, written to file immediately preceding the mysql_query() call in MediaWiki's database class.
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('mediawiki', $db);
$res = mysql_query(file_get_contents("query.sql"), $db);
while ($row = mysql_fetch_assoc($res)) {
var_dump($row);
}
echo mysql_error($db);
Edit: I imported a huge database dump and afterwards, when I loaded the PHP page, there was a noticeable wait that seemed to indicate that the query was running, but still no results showed. I ended up reworking the query, and I no longer have this problem.
Try this to detect and report errors better:
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('mediawiki', $db);
$res = mysql_query(file_get_contents("query.sql"), $db);
if (!$res) {
print "SQL Error ".mysql_errno().":".mysql_error().", from query: '".file_get_contents("query.sql")."'";
} else {
while ($row = mysql_fetch_assoc($res)) {
var_dump($row);
}
}
Try this after mysql_connect:
mysql_query('SET NAMES utf8;');