Need answer
Need answer

Reputation: 11

Mysql with C++ not printing all data

I am working on a MySql application using c++. I am not able to print all the records form my MySQL table. The code below is just printing the data of the first column.

  MYSQL *conn= mysql_init(NULL);
  MYSQL_RES *res; /* holds the result set */
  MYSQL_ROW row;
  if(mysql_real_connect(conn, "xx.1x3.1xx.7x", "ixxx", "00000000", "xxx", 0, NULL, 0)){
    char q[1000];
    mysql_query(conn, "select id FROM user");
    res = mysql_store_result(conn);
    int num_fields = mysql_num_fields(res);
    while ((row = mysql_fetch_row(res)))
    {
       // Print all columns
       for(int i = 0; i < num_fields; i++)
       {
           // Make sure row[i] is valid!
                char *val = row[i];
                std::cout << row[i][1] << std::endl;

                //std::cout << "NULL" << std::endl;

           // Also, you can use ternary operator here instead of if-else
           // cout << row[i] ? row[i] : "NULL" << endl;
       }
    }

  }

Upvotes: 0

Views: 1144

Answers (2)

Ivan M
Ivan M

Reputation: 61

Your query just selects the field id, it should be "select * from user"

Upvotes: 1

Need answer
Need answer

Reputation: 11

Sorry my bad figured it ahhh.. select id FROM user - > select * FROM user

Upvotes: 1

Related Questions