LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2005, 12:24 PM   #1
OrbiT^
LQ Newbie
 
Registered: Apr 2005
Posts: 5

Rep: Reputation: 0
C Segmenation fault


Hello,
I'm trying to write a tool that get's information out of the database from 2 tables. Could anybody tell me what's wrong with this code:

Thanks

Code:
#include "/usr/local/mysql/include/mysql/mysql.h"
#include <stdio.h>
#include <string.h>

main() {

   MYSQL *subconn;
   MYSQL *domeinconn;
   MYSQL_RES *sub;
   MYSQL_RES *domein;
   MYSQL_ROW subrow;
   MYSQL_ROW domeinrow;


   char *server = "xxxx";
   char *user = "xxxx";
   char *password = "xxxx";
   char *database = "xxxx";

   subconn = mysql_init(NULL);
   domeinconn = mysql_init(NULL);


   if (!mysql_real_connect(subconn, server,
         user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(subconn));
      exit(0);
   }

 if (!mysql_real_connect(domeinconn, server,
        user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(domeinconn));
      exit(0);
   }

   /* send SQL query */
   if (mysql_query(subconn, "SELECT * FROM isource_subdomeinen")) {
      fprintf(stderr, "%s\n", mysql_error(subconn));
      exit(0);
   }

   sub = mysql_use_result(subconn);
   domein = mysql_use_result(domeinconn);


   while ((subrow = mysql_fetch_row(sub)) != NULL) {

        if(mysql_query(domeinconn, "SELECT domein_naam FROM isource_domeinnamen WHERE domein_id=3")) {
                fprintf(stderr, "%s\n", mysql_error(domeinconn));
                exit(0);
        }

        while ((domeinrow = mysql_fetch_row(domein) != NULL) {
                printf("%s%s\n", domeinrow[0], subrow[2]);
        }

   }    

   mysql_close(domeinconn);
   mysql_close(subconn);
}

Last edited by OrbiT^; 04-19-2005 at 01:40 PM.
 
Old 04-19-2005, 01:38 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Well, there seems to be a missing end brace to the while((subrow = ... loop. Did you miss pasting one before the mysql_close() calls?
 
Old 04-19-2005, 01:39 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
In any event, when you get a segv the first thing to do is get figure out what line it happens on, either with printfs or (preferably) by gdb on the core file.
 
Old 04-19-2005, 01:39 PM   #4
OrbiT^
LQ Newbie
 
Registered: Apr 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by aluser
Well, there seems to be a missing end brace to the while((subrow = ... loop. Did you miss pasting one before the mysql_close() calls?
yes sorry, forgot to paste that, the brace is not causing the segfault.

gdb doesn't ouput the line, it just tell's me that the error is in the main structure.

Last edited by OrbiT^; 04-19-2005 at 01:41 PM.
 
Old 04-19-2005, 01:42 PM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Quote:
gdb doesn't ouput the line, it just tell's me that the error is in the main structure.
Are you compiling with -g?
 
Old 04-19-2005, 01:43 PM   #6
OrbiT^
LQ Newbie
 
Registered: Apr 2005
Posts: 5

Original Poster
Rep: Reputation: 0
No, should i do that?
 
Old 04-19-2005, 01:44 PM   #7
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Yes. : ) It includes debugging information in the executable so gdb can get line numbers and variable names.
 
Old 04-19-2005, 01:48 PM   #8
OrbiT^
LQ Newbie
 
Registered: Apr 2005
Posts: 5

Original Poster
Rep: Reputation: 0
This is what gdb ouput's:

Code:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1210264160 (LWP 22394)]
0xb7faa592 in mysql_fetch_row () from /usr/lib/libmysqlclient.so.14
(gdb)
 
Old 04-19-2005, 01:52 PM   #9
OrbiT^
LQ Newbie
 
Registered: Apr 2005
Posts: 5

Original Poster
Rep: Reputation: 0
aha i've got it to work
Thank you aluser

The line "domein = mysql_use_result(domeinconn);" had to be under the second query.
Stupid me i didn't see that
 
Old 04-19-2005, 02:58 PM   #10
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Ah, I spaced for a few minutes there. Next instruction would have been to type "bt" in gdb to get a backtrace : )
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault with ls -l Sledge Fedora 8 03-29-2005 08:02 PM
segmentation fault tej Linux - Hardware 1 02-08-2005 12:27 AM
Yahoo Messenger Segmenation Fault Crashed_Again Linux - Software 7 05-06-2003 06:13 AM
Segmenation Fault; Whats wrong!? f0d Programming 4 03-07-2002 01:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration