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 :
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.