Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6980719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:03:46+00:00 2026-05-27T18:03:46+00:00

Working with Visual Studio, Windows 7 and mysql.h library. What I want to do

  • 0

Working with Visual Studio, Windows 7 and mysql.h library.

What I want to do is send a MySQL query like this:

mysql_query(conn, "SELECT pass FROM users WHERE name='Leo Tolstoy'");

The only thing I can’t get working is sending a query where the name would be not a constant as it’s shown above, but a variable taken from a text field or anything else. So how should I work with a variable instead of a constant?

Hope I made my question clear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:03:46+00:00Added an answer on May 27, 2026 at 6:03 pm

    Use a prepared statement, which lets you parameterize values, similar to how functions let you parameterize variables in statement blocks. If using MySQL Connector/C++:

    // use std::unique_ptr, boost::shared_ptr, or whatever is most appropriate for RAII
    // Connector/C++ requires boost, so 
    std::unique_ptr<sql::Connection> db;
    std::unique_ptr<sql::PreparedStatement> getPassword
    std::unique_ptr<sql::ResultSet> result;
    std::string name = "Nikolai Gogol";
    std::string password;
    
    ...
    
    getPassword = db->prepareStatement("SELECT pass FROM users WHERE name=? LIMIT 1");
    
    getPassword->setString(1, name);
    result = getPassword->execute();
    if (result->first()) {
        password = result->getString("pass");
    } else {
        // no result
        ...
    }
    
    // smart pointers will handle deleting the sql::* instances
    

    Create classes to handle database access and wrap that in a method, and the rest of the application doesn’t even need to know that a database is being used.

    If you really want to use the old C API for some reason:

    MYSQL *mysql;
    ...
    
    const my_bool yes=1, no=0;
    const char* getPassStmt = "SELECT password FROM users WHERE username=? LIMIT 1";
    MYSQL_STMT *getPassword;
    MYSQL_BIND getPassParams;
    MYSQL_BIND result;
    
    std::string name = "Nikolai Gogol";
    std::string password;
    
    if (! (getPassword = mysql_stmt_init(mysql))) {
        // error: couldn't allocate space for statement
        ...
    }
    if (mysql_stmt_prepare(getPassword, getPassStmt, strlen(getPassStmt))) {
        /* error preparing statement; handle error and 
           return early or throw an exception. RAII would make
           this easier.
        */
        ...
    } else {
        unsigned long nameLength = name.size();
        memset(&getPassParams, 0, sizeof(getPassParams));
        getPassParams.buffer_type = MYSQL_TYPE_STRING;
        getPassParams.buffer = (char*) name.c_str();
        getPassParams.length = &nameLength;
    
        if (mysql_stmt_bind_param(getPassword, &getPassParams)) {
            /* error binding param */
            ...
        } else if (mysql_stmt_execute(getPassword)) {
            /* error executing query */
            ...
        } else {
            // for mysql_stmt_num_rows()
            mysql_stmt_store_result(getPassword);
            if (mysql_stmt_num_rows(getPassword)) {
                unsigned long passwordLength=0;
                memset(&result, 0, sizeof(result));
                result.length = &passwordLength;
                mysql_stmt_bind_result(getPassword, &result);
    
                mysql_stmt_fetch(getPassword);
                if (passwordLength > 0) {
                    result.buffer = new char[passwordLength+1];
                    memset(result.buffer, 0, passwordLength+1);
                    result.buffer_length = passwordLength+1;
                    if (mysql_stmt_fetch_column(getPassword, &result, 0, 0)) {
                        ...
                    } else {
                        password = static_cast<const char*>(result.buffer);
                    }
                }
            } else {
                // no result
                cerr << "No user '" << name << "' found." << endl;
            }
        }
        mysql_stmt_free_result(getPassword);
    }
    mysql_stmt_close(getPassword);
    
    mysql_close(mysql);
    

    As you see, Connector/C++ is simpler. It’s also less error prone; I probably made more mistakes using the C API than Connector/C++.

    See also:

    • Developing Database Applications Using MySQL Connector/C++
    • Connector C++ in the MySQL Forge wiki
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with visual studio 2008 developing software for windows CE 6.0, compact framework.
I'm working on a visual studio 2005 vb.net windows forms project that's been around
I am working with a Visual Studio 2008 (C#) project on Windows XP Pro.
I'm working on a C# windows program with Visual Studio 2008. Usually, I work
I'm working with an old windows app in visual studio 2005. A webserviced referenced
We are working on a Windows Mobile 6.5 application in Visual Studio 2008. The
right now we are starting working with Visual Studio 2010 and Windows Workflow Foundation
I'm using Visual Studio 2010 on Windows 7 32bit, and I'm working on my
I'm working on Windows XP with Visual Studio 2005.My project is a Cmake project
I am working with Visual Studio 2005 (C#) and in one of my windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.