Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi
I'm working on a project in C with mysql and I'm having a problem connecting to the MySQL server. My first connection works. However,
in another section I also need to retrieve info from the database as well. I am using the same code to make this other connection.
C#
conn = mysql_init(NULL);
//check if there is a connection
if (conn == NULL)
{
    printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
    exit(1);
}
    
if (mysql_real_connect(conn, "localhost", "test", "test", "test", 0, NULL, 0) == NULL)
{
    printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
}


When I use this a second time I get the following error:
Unhandled exception at 0x009818c9 in simple.exe: 0xC0000005: Access violation reading location 0x00000000.


it crashes on the if(conn == NULL)

Does anyone have an idea why this doesn't work?

Thanks.
Posted
Updated 16-Jun-11 0:26am
v3
Comments
Kim Togo 16-Jun-11 9:30am    
Please use "Add Comment" or else there is no notify email sent :-)

1 solution

You have to call mysql_close on the conn, before you call mysql_init
 
Share this answer
 
Comments
Kim Togo 16-Jun-11 9:29am    
[From OP]
i've tried that..
but that doesn't seem to work...
the strange thing is when it to program reaches the first connection...
conn already contains the connect info @ if (conn == NULL)
so it shouldn't enter there...
although it crashes at the printf line without writing in the command...
Kim Togo 16-Jun-11 9:31am    
Try use a new "conn", like "conn2". But do you need to have 2 connection open to MySql ?
j0r 16-Jun-11 10:30am    
that i also tried...
i now first close conn and conn2 before making any connection
but when i debug @ mysql_init(NULL) it already know the connection settings...
shouldn't this be null?
Kim Togo 16-Jun-11 10:54am    
No. Closing connection on conn does not set it to NULL. You have to do it you self.
j0r 16-Jun-11 14:22pm    
hmm i think conn = mysql_init(NULL); should take care of that? no?

i need to use the connection to the database in two different parts of the code...
the first time to check the database and return some values...

a second time to add some values that reffer to the value retrieved the first time...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900