mysqli_query返回false而不是1行

时间:2019-06-18 12:09:34

标签: mysql

我不明白为什么mysqli_query返回false,我在互联网上搜索很多,但是 我什么都没找到。

我的代码:

import os
import csv
import pandas as pd


from datetime import datetime

def normalizeDateString(ds):
# normalizes a date of format "d / d / dddd " to "dd/dd/dddd" ```
    sp = ds.replace(" ","").split("/")
    if len(sp[0])==1:
        sp[0]="0"+sp[0]
    if len(sp[1])==1:
        sp[1]="0"+sp[1]

    return sp[0]+"/"+sp[1]+"/"+sp[2]

def parseDT(dateString):
# parses "dd/dd/yyyy" as US (month/day/year). Fallback to (day/month/year) on error'''
    try:
        repl =  normalizeDateString(dateString)        
        return datetime.strptime(repl, "%m/%d/%Y").date()

    except:
        return datetime.strptime(repl, "%d/%m/%Y").date()


cwd = os.getcwd()

directory = cwd + '\\'

delheadfiles = ['USR02', 'USR06']

for delheadfile in delheadfiles:   
    for file in os.listdir(directory):
        if file.endswith(delheadfile + "_FINAL.csv"):       
            data = pd.read_csv(directory + delheadfile +  '_FINAL.csv', sep=",", low_memory=False, encoding='latin-1')
            data['GLTGB'].apply(parseDT)
            print(data)
            data.to_csv(directory + delheadfile +'_FINAL.csv', sep=',', index=False)

错误是:

  

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,在第55行的...中给出的布尔值

此查询始终返回$M1 = "SET @Month = month(TIMESTAMPADD(month, -1, CAST(now() AS DATE))); Set @firstDay = CONCAT('01','/', @month, '/',year(now())); SET @lastDay = last_day(TIMESTAMPADD(month, -1, CAST(now() AS DATE))); SELECT count(*) AS M1 FROM APPEL where CAST(D_CREATION AS DATE) BETWEEN @firstDay and @lastDay"; $connexion = cnx(); if ($connexion) { $result = mysqli_query($connexion, $M1); if($result) { $row = mysqli_fetch_assoc($result); // This is Line 55 $_SESSION['ArrayMois'][0] = implode($row); } else { $_SESSION['ArrayMois'][0] = 0; } } ,但是如果我在PHPMyAdmin中执行该查询,则会返回一行。

1 个答案:

答案 0 :(得分:1)

1)MySQL reporting log locations

2)阅读this answer,了解如何生成和阅读MySQL错误报告。

输入您的代码

$row = mysqli_fetch_assoc($result) 
      or error_log("MYSQL Error on line ". __LINE__. 
         " :: ".print_r(mysqli_error($connection),true));

更新:

您声明:

  

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,在第55行的...中给出的布尔值

您的错误含义:

这意味着 Query 未能按预期工作:

$result = mysqli_query($connexion, $M1) 
          or error_log("MYSQL Error on line ". __LINE__. 
             " :: ".print_r(mysqli_error($connection),true));

问题:

您正在一个MySQL_query查询处理程序中运行多个单独的SQL查询,默认情况下,在大多数情况下,这是不允许的。也许您已经设置了SQL以接受级联查询,但是通常这是不明智的,而且可能性很小。

替代。您可以创建一个PROCEDURE, FUNCTION or similar

您也不要将列名包装在反引号中,这是非常可取的。


修改您的SQL:

当前您拥有:

SET @Month = month(TIMESTAMPADD(month, -1, CAST(now() AS DATE)));  

Set @firstDay = CONCAT('01','/', @month, '/',year(now())); 

SET @lastDay = last_day(TIMESTAMPADD(month, -1, CAST(now() AS DATE))); 

SELECT count(*) AS M1 FROM APPEL where CAST(D_CREATION AS DATE) 
       BETWEEN @firstDay and @lastDay";

这可以写得更简洁:

SELECT COUNT(*) as M1 FROM APPEL WHERE CAST(`D_CREATION` AS DATE) BETWEEN
(LAST_DAY(CURDATE() - INTERVAL 1 MONTH) + INTERVAL 1 DAY) AND LAST_DAY(CURDATE())

这将列出当月第一天和最后一天之间的所有结果。从您的代码中还不清楚您到底想达到什么目的,但是您似乎希望获得上一个月的列表,在这种情况下,您可以使用:

SELECT COUNT(*) as M1 FROM APPEL WHERE CAST(`D_CREATION` AS DATE) BETWEEN
(LAST_DAY(CURDATE() - INTERVAL 2 MONTH) + INTERVAL 1 DAY) 
AND LAST_DAY(CURDATE() - INTERVAL 1 MONTH)