查询在PHP中返回空结果,但在phpmyadmin上工作正常

时间:2018-07-12 21:40:45

标签: php mysqli

您能帮我些什么吗?这是我的代码,应该从MYSQL DB返回值。问题是当我打印结果(数组)时它是空的。

$query = 'call test("Ingreso","Ventas Exportación",2015,1,6,"Ventas Expo")';
echo $query;
$query = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($query);

(
[sum_ppto] => 
[sum_real] => 
[sum_real_ytd_pasado] => 
[ppto_anual] => 
[ppto_meses_restantes] => 
)

但是当我执行完全相同的查询时,如果没有参数,则数组中将填充信息。

$query = 'call test2()';
echo $query;
$query = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($query);
(
    [sum_ppto] => 2987676043
    [sum_real] => 3115640330
    [sum_real_ytd_pasado] => 2843994399
    [ppto_anual] => 5217552938
    [ppto_meses_restantes] => 2101912608
)

我在phpmyadmin中运行例程(测试)时的图片在哪里 Click Here

这是另一个例程的图片(test2) Click here

程序是这个

select 
(select 
sum(Valor) 
from 
registro
where 
Anio_idAnio=year and
Mes_idMes between mes_inicio and mes_termino and
idReal_Ppto = (select idReal_Ppto from real_ppto where Real_Ppto = "Ppto") 
and 
idArea_Negocio=(select idArea_Negocio from area_negocio where Area_Negocio = 
area_negocio_input) and 
idNom_CC = (select idNom_CC from nom_cc where Nom_CC=nom_cc_input) and
idG_Cta = (select idG_Cta from g_cta where G_Cta=g_cta_input)) 
    as sum_ppto,
(select 
sum(Valor) 
from 
registro
where 
Anio_idAnio=year and
Mes_idMes between mes_inicio and mes_termino and
idReal_Ppto = (select idReal_Ppto from real_ppto where Real_Ppto = "Real") 
and 
idArea_Negocio=(select idArea_Negocio from area_negocio where Area_Negocio = 
area_negocio_input) and 
idNom_CC = (select idNom_CC from nom_cc where Nom_CC=nom_cc_input) and
idG_Cta = (select idG_Cta from g_cta where G_Cta=g_cta_input)) 
    as sum_real,
(select 
sum(Valor) 
from 
registro
where 
Anio_idAnio=year-1 and
Mes_idMes between mes_inicio and mes_termino and
idReal_Ppto = (select idReal_Ppto from real_ppto where Real_Ppto = "Real") 
and 
idArea_Negocio=(select idArea_Negocio from area_negocio where Area_Negocio = 
area_negocio_input) and 
idNom_CC = (select idNom_CC from nom_cc where Nom_CC=nom_cc_input) and
idG_Cta = (select idG_Cta from g_cta where G_Cta=g_cta_input))
    as sum_real_ytd_pasado,
(select 
sum(Valor) 
from 
registro
where 
Anio_idAnio=year and
Mes_idMes between 1 and 12 and
idReal_Ppto = (select idReal_Ppto from real_ppto where Real_Ppto = "Real") 
and 
idArea_Negocio=(select idArea_Negocio from area_negocio where Area_Negocio = 
area_negocio_input) and 
idNom_CC = (select idNom_CC from nom_cc where Nom_CC=nom_cc_input) and
idG_Cta = (select idG_Cta from g_cta where G_Cta=g_cta_input))
    as ppto_anual,
(select 
sum(Valor) 
from 
registro
where 
Anio_idAnio=year and
Mes_idMes not between 1 and mes_termino and
idReal_Ppto = (select idReal_Ppto from real_ppto where Real_Ppto = "Real") 
and 
idArea_Negocio=(select idArea_Negocio from area_negocio where Area_Negocio = 
area_negocio_input) and 
idNom_CC = (select idNom_CC from nom_cc where Nom_CC=nom_cc_input) and
idG_Cta = (select idG_Cta from g_cta where G_Cta=g_cta_input))
    as ppto_meses_restantes

请帮助!

谢谢!

1 个答案:

答案 0 :(得分:2)

查询正在运行,或者在运行CALL时不会获得键名。

因此问题必须出在选择子句中。

我看到的唯一可能的原因是参数"Ventas Exportación"由于在Esportacion中使用“ o”而被错误的字符集映射所破坏,并使SELECT不返回任何内容。 / p>

作为快速解决方案,您可以尝试将=子句替换为LIKE,然后将“ Ventas Esportac%”作为参数传递。或者查看是否可以使用其他不包含重音符号的值。如果这样有效,请尝试检查哪些字符集起作用。

@ Rainmx93用户建议的修复程序:

try adding mysqli_set_charset($conn, "utf8") before $query

我遇到了种种麻烦,因为拉丁文不匹配(我的安装中是MySQL的默认设置),又名ISO-8859-1(5),CP-1282和UTF8。未正确选择的行很常见。您可能需要更改数据库的排序规则(首先执行备份)。