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 9171301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:08:38+00:00 2026-06-17T16:08:38+00:00

I’m trying to check for certain field values in an if . Most of

  • 0

I’m trying to check for certain field values in an if. Most of these values are NULL in MySQL.

It seems to be screwing everything up.

I do

while ((row = mysql_fetch_row(res)) !=NULL){
    if(row[1] != "-1"){
        rows.push_back(row[0]);
    }
}

The field for row[1] is an INT LENGTH 1 equal to 1, -1, or NULL. Most are NULL, but a few are -1. I think that mysql.h outputs all values as char* (at least that’s the only way I’ve been able to get it to work so far).

Anyways, strangely, rows gets filled, but it’s filled with “nothing”. I’m not even sure if it’s an empty string or what.

Please help.

Many thanks in advance!


I put dashes in front and behind the std::cout of rows[i] in a for loop. It outputs a ton of --s.

If I std::cout the raw row[0], it outputs fine.


For us2012:

std::vector< char* > rows;

while ((row = mysql_fetch_row(res)) !=NULL){
    std::cout << "-" << row[0] << "-" << std::endl;
    if(row[1] != "-1"){
        rows.push_back(row[0]);
    }
}

std::cout << "Valids:" << std::endl;
for(int i = 0; i < rows.size(); ++i)
{
    std::cout << "id: -" << rows[i] << "-" << std::endl;
}

When I use this if

if(row[1] && row[1] != "-1")

The if works in reverse.

Note: I incremented an int in the while, couted that and rows and invalidRows. The counter gives 389, rows.size() 2, and invalidRows.size() 387.

I’m going to see if I can cout row[0] after I do the if…


couting row[0] after the if outputs the correct data.


if(row[1] != "-1") gives ...forbids comparison between pointer and integer...


Why mysql.h:

CentOS. I find hardly anything for it, and nothing my host supports.


if(row[1] && std::string(row[1]).compare("-1") != 0) put everything into invalidRows

Without row[] &&gavewhat(): basic_string::_S_construct NULL not valid Aborted`


Answer

It was if(row[1] && std::string(row[1]).compare("-1") == 0) all along! My logic got screwed up in all of the C++ crash coursing.

And using std::vector< std::string > allows row[x] to be pushed_back.

  • 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-06-17T16:08:39+00:00Added an answer on June 17, 2026 at 4:08 pm

    First things first: The API you are using is a C API. There’s nothing inherently wrong about that, but it means that you have to deal with a lot of stuff that seems unneccessary because
    C++ has better tools available for this. If possible, you should therefore use a C++ wrapper like MySQL++ : http://tangentsoft.net/mysql++/ (all credits for this suggestion go to @Non-Stop Time Travel in the comments below)


    In your original code, you’re not comparing strings, you’re comparing addresses. Your condition compares the memory address stored in row[1] with the memory address of the constant string literal "-1". What you actually want (if you stick to char *) is strcmp : C++ Compare char array with string

    You’re also pushing back pointers to row[0] which is temporary, it is reassigned with every iteration of the while loop! You have to make a copy of these and store the pointers to the copies.

    This is a very quick idea of how you could approach this. Not really good C++, though (in fact, if it were not for the vector and the cout, it may as well be C. See above for the reasons.):

    #include <string.h>
    
    std::vector<char*> rows;
    while ((row = mysql_fetch_row(res)) !=NULL){
        if(strcmp(row[1],"-1") != 0){
            char * store = new char[strlen(row[0])+1];
            strncpy(store ,row[0] ,strlen(row[0])+1);
            rows.push_back(store);
        }
    }
    std::cout << "Valids:" << std::endl;
    for(int i = 0; i < rows.size(); ++i)
    {
        std::cout << "id: -" << rows[i] << "-" << std::endl;
    }
    
    //clean up
    for(int i = 0; i < rows.size(); ++i)
    {
        delete [] rows[i];
    }
    

    In general, when you deal with strings in C++ I would recommend using std::string and std::string::compare instead, but this is problematic here – if your mysql rows hold binary data, you don’t have guaranteed null-terminated strings anymore (see the spec for MYSQL_ROW here: http://dev.mysql.com/doc/refman/5.0/en/c-api-data-structures.html ). As pointed out by Non-Stop Time Travel in the comments below, std::string supports internal \0s, the code example below however doesn’t work with them.

    #include <string>
    
    //this particular code works only if row[0] and row[1] are nullterminated
    
    std::vector<std::string> rows;
    while ((row = mysql_fetch_row(res)) !=NULL){
        if(std::string(row[1]).compare("-1") != 0){
            rows.push_back(std::string(row[0]));
        }
    }
    std::cout << "Valids:" << std::endl;
    for(int i = 0; i < rows.size(); ++i)
    {
        std::cout << "id: -" << rows[i] << "-" << std::endl;
    }
    
    //cleaning up the vector of strings is not necessary! yay!
    

    )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.