C++ MySQL在从注入转换到参数化时遇到问题

C++ MySQL在从注入转换到参数化时遇到问题,c++,mysql,C++,Mysql,我这里有以下执行查询的代码。最初,我使用SQL注入返回行结果。听说我应该使用参数化,我重新整理了我的代码,并阅读了MySQL文档中关于如何使用参数化的内容。我在C++应用程序中使用MySQL的C库。 然而,它并没有返回结果 我知道我的SQL语句是100%好的。它已经过测试。我唯一更改的是将%d(注入)更改为?,它接受玩家的ID 这返回-1。这是一个SELECT语句,所以可能是正常的 // Get the number of affected rows affected_rows

我这里有以下执行查询的代码。最初,我使用SQL注入返回行结果。听说我应该使用参数化,我重新整理了我的代码,并阅读了MySQL文档中关于如何使用参数化的内容。我在C++应用程序中使用MySQL的C库。 然而,它并没有返回结果

我知道我的SQL语句是100%好的。它已经过测试。我唯一更改的是将%d(注入)更改为?,它接受玩家的ID

这返回-1。这是一个SELECT语句,所以可能是正常的

    // Get the number of affected rows
    affected_rows = mysql_stmt_affected_rows(m_stmt);
这返回2。这是正确的。我有两个字段被返回

    // Store the field count
    m_fieldCount = mysql_field_count(&m_conn);
这将返回0(成功)

最后,返回null

    m_result = mysql_store_result(&m_conn);
我需要m_结果,以便我可以读取行。“mysql\u stmt\u store\u result”听起来类似,但不返回mysql\u结果

  m_result = mysql_store_result(&m_conn);

/// <summary>
/// Executes a query.
/// </summary>
/// <param name="query">The query to execute.</param>
/// <returns>Returns true on success, else false.</returns>
bool SQLConnection::executeQuery_New(const char *query)
{
    int param_count = 0;
    int affected_rows = 0;

    // Validate connection.
    if (!m_connected)
        return false; 

    // Initialize the statement
    m_stmt = mysql_stmt_init(&m_conn);
    if (!m_stmt) {
        fprintf(stderr, " mysql_stmt_init(), out of memory\n");
        return false;
    }

    // Prepare the statement
    if (mysql_stmt_prepare(m_stmt, query, strlen(query))) {
        fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(m_stmt));
        return false;
    }

    // Get the parameter count from the statement
    param_count = mysql_stmt_param_count(m_stmt);
    if (param_count != m_bind.size()) {
        fprintf(stderr, " invalid parameter count returned by MySQL\n");
        return false;
    }

    // Bind buffers 
    // The parameter binds are stored in std::vector<MYSQL_BIND>
    // I need to convert std::vector<MYSQL_BIND> m_bind to MYSQL_BIND *bnd
    MYSQL_BIND *bind = new MYSQL_BIND[m_bind.size()  + 1];

    memset(bind, 0, sizeof(bind) * m_bind.size()); 

    for (int i = 0; i < param_count; i++)
        bind[i] = m_bind[i]; 

    if (mysql_stmt_bind_param(m_stmt, &bind[0]))
    {
        fprintf(stderr, " mysql_stmt_bind_param() failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(m_stmt));
        return false;
    }

    // Execute the query
    if (mysql_stmt_execute(m_stmt)) {
        fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(m_stmt));
        return false;
    }

    // Get the number of affected rows
    affected_rows = mysql_stmt_affected_rows(m_stmt);
    //if (affected_rows == -1) {
    //  fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
    //  fprintf(stderr, " %s\n", mysql_stmt_error(m_stmt));
    //  return false;
    //}

    // Store the field count
    m_fieldCount = mysql_field_count(&m_conn);


    // Store the result
    if (mysql_stmt_store_result(m_stmt)) 
    {
        fprintf(stderr, " failed retrieving result\n");
        fprintf(stderr, " %s\n", mysql_error(&m_conn));
        int d = mysql_errno(&m_conn);
        return false;
    }

    // This looks similar to the last above statement, but I need m_result. I used mysql_store_result earlier when using injection and it worked fine, but here in this case it returns null. 
    m_result = mysql_store_result(&m_conn);


    // Close the statement
    if (mysql_stmt_close(m_stmt)) {
        /* mysql_stmt_close() invalidates stmt, so call          */
        /* mysql_error(mysql) rather than mysql_stmt_error(stmt) */
        fprintf(stderr, " failed while closing the statement\n");
        fprintf(stderr, " %s\n", mysql_error(&m_conn));
        return false; 
    }

    // Delete bind array
    if (bind) {
        delete[] bind;
        bind = NULL;
    }

    return true;
}
我的变量及其类型:

class SQLConnection
{
private: 
    MYSQL m_conn;
    MYSQL_ROW m_row;
    MYSQL_RES *m_result;  
    char m_errorMessage[ERROR_MSG_MAX];
    bool m_connected;
    MYSQL_STMT *m_stmt;
    std::vector<MYSQL_BIND> m_bind;
    int m_fieldCount;
    // ...
我得到的错误代码是2014:

只是出于兴趣,这是我使用的一个先验函数。这注射效果很好。使用上述带有参数化的新函数是导致问题的原因之一

bool SQLConnection::executeQuery(const char *query)
{ 
    // Validate connection.
    if (!m_connected)
        return false;  

    // Execute the query
    int status = mysql_query(&m_conn, query);

    if (status != 0) {
        sprintf(m_errorMessage, "Error: %s", mysql_error(&m_conn)); 
        return false;
    } 

    // Store the result
    m_result = mysql_store_result(&m_conn);

    return true;
}  
在我开始使用C语言来克服C++语言的宗教战争之后,我想我会在这里做最后一次尝试。感谢您的帮助

编辑: 这是我在参数化之前读取列名的方式(可能在调用mysql_stmt_store_result(m_stmt)之后需要更新此代码)

std::string SQLConnection::getField(const char*fieldName)
{
MYSQL_FIELD*FIELD=NULL;
无符号整数名\字段=0;
mysql_stmt_data_seek(m_stmt,0);
mysql_stmt_fetch_列(m_stmt,&bind,0,0);
//mysql_data_seek(m_result,0);
//mysql_字段_seek(m_结果,0);
const unsigned int num_fields=mysql_stmt_field_count(m_stmt);
//const unsigned int num_fields=mysql_num_fields(m_结果);
std::向量头(num_字段);
for(unsigned int i=0;(field=mysql_fetch_field(m_result));i++)
{
标题[i]=字段->名称;
if(strcmp(字段名,头[i])==0)
name_field=i;
}  
而((m_row=mysql_fetch_row(m_result))){
返回标准::字符串(m_行[名称_字段]);
}
返回“”;
}
编辑:
我发现在最后一个函数中,语句有等价的函数,比如mysql\u num\u fields()就是mysql\u stmt\u field\u count()。我认为这些需要更新,因为它使用的是m_stmt而不是m_result,这就给了我们更新函数以便使用m_stmt的理由。但是,如何更新函数的后半部分还不是很清楚。

您可能需要更好地理解
stmt
的工作原理。您无法通过
mys获得最终结果ql_store_result()
当您使用
stmt

  • 您应该为用于接受结果集的语句绑定几个缓冲区。您可以通过以下方式完成此操作,就像
    mysql\u stmt\u bind\u param()

  • 然后,您可以使用绑定的缓冲区返回行数据

  • 重复执行fetch方法,以便可以逐行获取整个结果集

  • 调用的基本顺序:

    • mysql\u stmt\u init
    • mysql\u stmt\u准备
    • mysql\u stmt\u bind\u参数
    • mysql\u stmt\u执行
    • mysql\u stmt\u bind\u结果
    • mysql\u stmt\u存储\u结果
    • mysql_stmt_fetch(重复,逐行)
    • mysql\u stmt\u free\u结果
    它对我有用

    我已经有很长时间没有完成我的这部分项目了,你最好仔细阅读手册,找到更多的
    stmt
    示例


    对不起,我的英语很差。祝你好运!

    你可能需要更好地理解
    stmt
    的工作原理。当你使用
    stmt
    时,你无法通过
    mysql\u store\u result()
    获得最终结果

  • 您应该为用于接受结果集的语句绑定几个缓冲区。您可以通过以下方式完成此操作,就像
    mysql\u stmt\u bind\u param()

  • 然后,您可以使用绑定的缓冲区返回行数据

  • 重复执行fetch方法,以便可以逐行获取整个结果集

  • 调用的基本顺序:

    • mysql\u stmt\u init
    • mysql\u stmt\u准备
    • mysql\u stmt\u bind\u参数
    • mysql\u stmt\u执行
    • mysql\u stmt\u bind\u结果
    • mysql\u stmt\u存储\u结果
    • mysql_stmt_fetch(重复,逐行)
    • mysql\u stmt\u free\u结果
    它对我有用

    我已经有很长时间没有完成我的这部分项目了,你最好仔细阅读手册,找到更多的
    stmt
    示例

    对不起,我的英语很差。祝你好运

    class SQLConnection
    {
    private: 
        MYSQL m_conn;
        MYSQL_ROW m_row;
        MYSQL_RES *m_result;  
        char m_errorMessage[ERROR_MSG_MAX];
        bool m_connected;
        MYSQL_STMT *m_stmt;
        std::vector<MYSQL_BIND> m_bind;
        int m_fieldCount;
        // ...
    
    ...WHERE player_id = ?;"); 
    
    conn.addParam(m_id, MYSQL_TYPE_LONGLONG, 0);
    
        if (!conn.executeQuery_New(buffer)) {
            conn.close();
            return "";
        }
    
        // Close the connection.
        conn.close();
    
        std::string s = conn.getField("max_value_column_name");
    
    bool SQLConnection::executeQuery(const char *query)
    { 
        // Validate connection.
        if (!m_connected)
            return false;  
    
        // Execute the query
        int status = mysql_query(&m_conn, query);
    
        if (status != 0) {
            sprintf(m_errorMessage, "Error: %s", mysql_error(&m_conn)); 
            return false;
        } 
    
        // Store the result
        m_result = mysql_store_result(&m_conn);
    
        return true;
    }  
    
    std::string SQLConnection::getField(const char *fieldName)
    {
        MYSQL_FIELD *field = NULL;
        unsigned int name_field = 0;
    
         mysql_stmt_data_seek(m_stmt, 0);
         mysql_stmt_fetch_column(m_stmt, &bind, 0, 0);
         //mysql_data_seek(m_result, 0);
         //mysql_field_seek(m_result, 0);
    
         const unsigned int num_fields = mysql_stmt_field_count(m_stmt); 
         // const unsigned int num_fields = mysql_num_fields(m_result);
    
        std::vector<char *> headers(num_fields);
    
        for (unsigned int i = 0; (field = mysql_fetch_field(m_result)); i++)
        {
            headers[i] = field->name;
    
            if (strcmp(fieldName, headers[i]) == 0)
                name_field = i;
        }  
    
        while ((m_row = mysql_fetch_row(m_result))) {
            return std::string(m_row[name_field]);
        }
    
        return "";
    }