capcom
capcom

Reputation: 3337

Why am I getting a segmentation fault (core dumped)?

This is the code I am trying to run. It compiles fine, and worked great until yesterday.

#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv)
{

  MYSQL *conn;
  MYSQL_RES *result;
  MYSQL_ROW row;
  int num_fields;
  int i;

  conn = mysql_init(NULL);
  mysql_real_connect(conn, "hostname", "username", "password", "database_name", 0, NULL, 0);

  mysql_query(conn, "SELECT * FROM tabletest");
  result = mysql_store_result(conn);

  num_fields = mysql_num_fields(result);

  while ((row = mysql_fetch_row(result)))
  {
      for(i = 0; i < num_fields; i++)
      {
          printf("%s ", row[i] ? row[i] : "NULL");
      }
      printf("\n");
  }

  mysql_free_result(result);
  mysql_close(conn);

}

Note that the parameters for mysql_real_connect() are generic here for privacy, but like I said, it worked yesterday. When I try to run the code after compiling successfully, I get:

Segmentation fault (core dumped)

Upvotes: 3

Views: 6044

Answers (1)

capcom
capcom

Reputation: 3337

Paddy pointed out the key problem, I didn't error check. After error checking, I found out that I wasn't being granted access to my remote server and was getting

Error 1045: Access denied for user 'username'@'ip' (using password: YES)

I then realized that the IP address of my computer changed after turning it on. I never knew this happened. So I had to go back into cPanel, and add my 'new' IP address to the remote access list for MySQL, and it works. Now the issue is to find out how my IP address can stay static.

The moral of the story is to always handle errors.

Upvotes: 4

Related Questions