| | |
Authenticate users for mysql database using C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 30
Reputation:
Solved Threads: 0
Hi, everyone!
I want to make the user authentication for mysql database. I write the code and test it, but can't run. Let's I talk about my project idea first.
I have a server, a client and a mysql database. Before entering the server, the user must login first, After login, he can access the database using his privileges (can create databases,tables, insert , update and retrieve data). After finishing the desired tasks, he can log out, then the server will reflects his status as log out.
My code for authentication part is :
I write like the above code, it does not work. But if I replace the host, username and password by the string, it works. But I don't want the constant username and password, because many users will log in to the server. So what do I need to do? Please help me with the example code, if any.
Also when the password is entered, I want it to be invisible to the others, what should I add in?
And if the user has few databases, can I let him log in first, without choosing the database? After login, he can work on the wanted database (same when working with mysql monitor screen, choosing database name by command) Because of the mysql_real_connect definition, I can't let databasename blank, but it may be difficult for the user, if he does not remember all the databases he has.
Thank a lot for your help and your time.
I want to make the user authentication for mysql database. I write the code and test it, but can't run. Let's I talk about my project idea first.
I have a server, a client and a mysql database. Before entering the server, the user must login first, After login, he can access the database using his privileges (can create databases,tables, insert , update and retrieve data). After finishing the desired tasks, he can log out, then the server will reflects his status as log out.
My code for authentication part is :
C Syntax (Toggle Plain Text)
static char *opt_host_name = NULL; static char *opt_user_name = NULL; /* username (default=login name) */ static char *opt_password = NULL; /* password (default=none) */ static char *opt_db_name = NULL; /* database name (default=none) */ int main((int argc, char *argv[]){ printf("Enter host name : "); fflush(stdin); gets(opt_host_name); printf("Enter user name : "); fflush(stdin); gets(opt_user_name); printf("Enter password : "); fflush(stdin); gets(opt_password); printf("and database : "); fflush(stdin); gets(opt_db_name); if((mysql=mysql_init(mysql))==NULL) { printf("\nFailed to initate MySQL connection"); exit(1); } if (!mysql_real_connect(mysql,opt_host_name,opt_user_name,opt_password, opt_db_name,0,NULL,0)) { printf( "Failed to connect to MySQL: Error: %s\n", mysql_error(mysql)); exit(1); } printf("\nLogged on to database sucessfully\n\n"); ..... }
I write like the above code, it does not work. But if I replace the host, username and password by the string, it works. But I don't want the constant username and password, because many users will log in to the server. So what do I need to do? Please help me with the example code, if any.
Also when the password is entered, I want it to be invisible to the others, what should I add in?
And if the user has few databases, can I let him log in first, without choosing the database? After login, he can work on the wanted database (same when working with mysql monitor screen, choosing database name by command) Because of the mysql_real_connect definition, I can't let databasename blank, but it may be difficult for the user, if he does not remember all the databases he has.
Thank a lot for your help and your time.
Last edited by Hannahlv; Aug 30th, 2008 at 5:20 am.
Nevermind the database, you need to work on the basics of C programming.
> int main((int argc, char *argv[]){
Mis-matched.
You didn't even compile this before posting did you.
> fflush(stdin);
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
> gets(opt_host_name);
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
Not only that, where is opt_host_name pointing?
Answer - nowhere.
Practice reading input using char arrays and calls to fgets() for a while.
> int main((int argc, char *argv[]){
Mis-matched.
You didn't even compile this before posting did you.
> fflush(stdin);
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
> gets(opt_host_name);
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
Not only that, where is opt_host_name pointing?
Answer - nowhere.
Practice reading input using char arrays and calls to fgets() for a while.
•
•
Join Date: Jul 2008
Posts: 30
Reputation:
Solved Threads: 0
Ok. I know my mistakes in those 2 calls now (I always use like this, thank you for correcting). But your question about the opt_host_name, where is the pointer pointing, I want to ask another question (I'm not so good at programming). The mysql_real_connect definition is :
All the parameters are pointer. If I don't declare variable as a pointer, all those will be incompatible. (is it right? This is my thinking that they must be the same in type of variable). If not correct, how can I change them?
C Syntax (Toggle Plain Text)
mysql_real_connect : (* if MYSQL_VERSION_ID >= 32200 *) mysql -> string (* const char* host *) -> string (* const char* user *) -> string (* const char* passwd *) -> string (* const char* db *) -> int (* unsigned int port *) -> string (* const char* unix_socket *) -> int (* unsigned int client_flag *) -> mysql
All the parameters are pointer. If I don't declare variable as a pointer, all those will be incompatible. (is it right? This is my thinking that they must be the same in type of variable). If not correct, how can I change them?
Say you have
You can call it with ALL of these
In the context of a function formal parameter (or a prototype), what it is telling you is that the function will NOT change it.
It means that whatever was in your array before the call will still be there after the call.
What you call it with doesn't have to be const as well.
Similarly, when you call a function expecting an int pointer (for example), then this is what it usually means
Now you can declare a pointer it you want, but you need to initialise it.
But this is definitely wrong
Wherever you have a pointer, you need to be thinking about where the memory is, and have you initialised it to point to some suitable memory. Simply declaring variables with the correct type will keep the compiler from complaining (sure the types are right), but it will always blow up as soon as you run it.
void foo ( const char *param ); You can call it with ALL of these
C Syntax (Toggle Plain Text)
char buff[100]; const char cbuff[] = "hello"; char *pbuff = buff; const char *pcbuff = cbuff; foo ( buff ); foo ( cbuff ); foo ( pbuff ); foo ( pcbuff ); foo( "hello world" );
In the context of a function formal parameter (or a prototype), what it is telling you is that the function will NOT change it.
C Syntax (Toggle Plain Text)
void bar ( const int *p, int *q ) { *p = 0; // NOT allowed, p is a pointer to const *q = 0; // this one is OK }
What you call it with doesn't have to be const as well.
Similarly, when you call a function expecting an int pointer (for example), then this is what it usually means
C Syntax (Toggle Plain Text)
int result; func ( &result );
Now you can declare a pointer it you want, but you need to initialise it.
C Syntax (Toggle Plain Text)
int result; int *pa = &result; func ( pa );
But this is definitely wrong
C Syntax (Toggle Plain Text)
int *pa; func ( pa );
Wherever you have a pointer, you need to be thinking about where the memory is, and have you initialised it to point to some suitable memory. Simply declaring variables with the correct type will keep the compiler from complaining (sure the types are right), but it will always blow up as soon as you run it.
•
•
Join Date: Jul 2008
Posts: 30
Reputation:
Solved Threads: 0
Ok. I know my mistakes in those 2 calls now (I always use like this, thank you for correcting). But your question about the opt_host_name, where is the pointer pointing, I want to ask another question (I'm not so good at programming). The mysql_real_connect definition is :
All the parameters are pointer. If I don't declare variable as a pointer, all those will be incompatible. (is it right? This is my thinking that they must be the same in type of variable). If not correct, how can I change them?
P/S : Sorry for second post of the same content, just a mistake. Thank for your reply. Anything else don't understand I'll re-post. Hope it don't bother you.
C Syntax (Toggle Plain Text)
mysql_real_connect : (* if MYSQL_VERSION_ID >= 32200 *) mysql -> string (* const char* host *) -> string (* const char* user *) -> string (* const char* passwd *) -> string (* const char* db *) -> int (* unsigned int port *) -> string (* const char* unix_socket *) -> int (* unsigned int client_flag *) -> mysql
All the parameters are pointer. If I don't declare variable as a pointer, all those will be incompatible. (is it right? This is my thinking that they must be the same in type of variable). If not correct, how can I change them?
P/S : Sorry for second post of the same content, just a mistake. Thank for your reply. Anything else don't understand I'll re-post. Hope it don't bother you.
Last edited by Hannahlv; Aug 30th, 2008 at 12:14 pm.
•
•
Join Date: Jul 2008
Posts: 30
Reputation:
Solved Threads: 0
Er...I have one problem with mysql database. Hope you can guide me, Salem.
The second parameter is a string. But in this case it's hard-coded. I want to take the user input and assign to the second parameter. I wrote the code like this :
But it even not let me to enter anything, directly go to SQL statement (but of course can't work). I write the code to get the user input separate, to another file to test, it works. So what's the problem here?
C Syntax (Toggle Plain Text)
if (mysql_query(conn, "show tables")) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); }
The second parameter is a string. But in this case it's hard-coded. I want to take the user input and assign to the second parameter. I wrote the code like this :
C Syntax (Toggle Plain Text)
char sQuery[1000]; printf("msyql>"); fgets(sQuery, sizeof sQuery, stdin); if (mysql_query(conn, sQuery)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); if(mysql_query(conn, sQuery) !=0) { print_error (conn, "UPDATE statement failed"); } else { printf("CREATE statement succeeded."); }
But it even not let me to enter anything, directly go to SQL statement (but of course can't work). I write the code to get the user input separate, to another file to test, it works. So what's the problem here?
Last edited by Hannahlv; Aug 31st, 2008 at 6:57 am.
•
•
Join Date: Jul 2008
Posts: 30
Reputation:
Solved Threads: 0
I copy those lines :
to another text editor. Result is, it can display what I enter. But put together with the SQL statement, can't work. I just don't know what the reason is. Also try remove the "\n", the problem's still there.
C Syntax (Toggle Plain Text)
char sQuery[1000]; printf("msyql>"); fgets(sQuery, sizeof sQuery, stdin); printf("%s", sQuery); //to see if fgets() works
Last edited by Hannahlv; Aug 31st, 2008 at 11:53 am.
Do you call any other input routines like say scanf ?
Because that can mess you up as well.
Eg.
Ideally, you should use fgets() for ALL input, then use say sscanf(), or whatever else to parse the input.
But in the absense of that, try calling this between scanf() and fgets() calls.
Because that can mess you up as well.
Eg.
C Syntax (Toggle Plain Text)
scanf( "%d", &myint ); fgets( buff, sizeof buff, stdin ); // this will "appear" to be skipped.
Ideally, you should use fgets() for ALL input, then use say sscanf(), or whatever else to parse the input.
But in the absense of that, try calling this between scanf() and fgets() calls.
C Syntax (Toggle Plain Text)
int skipToEOL ( FILE *s ) { int ch; while ( (ch=fgetc(s)) != EOF && ch != '\n' ); return ch; }
![]() |
Similar Threads
- How do I create a Members Area (IT Professionals' Lounge)
- Captive portals (Networking Hardware Configuration)
- question about connecting odbc to sql through php script (PHP)
- DB-Authentication php/mysql on Mac OSX v3 (PHP)
Other Threads in the C Forum
- Previous Thread: change a folder name
- Next Thread: lex/flex in C
| Thread Tools | Search this Thread |
* adobe api append array arrays bash binarysearch centimeter char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lowest match matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user whythiscodecausesegmentationfault win32api windows.h






