MySQL problem
Karel Dudasek
karel.dudasek na vghur.army.cz
Úterý Březen 16 12:49:37 CET 2004
On Tue, 16 Mar 2004, Miloš Lesák wrote:
> Cast kodu sql_auth.c aj s pouzitim mysql_connect:
> -------------------------------------------
> #include <string.h>
> #include <stdio.h>
> //#include <mysql/mysql.h>
> #include <mysql.h>
> #include <time.h>
>
> #define SERVER "localhost"
> #define SERVER_LOGIN "user"
> #define SERVER_PASS "pass"
>
> int check_time( char *accessString );
> int auth_ok( MYSQL *srvr, char *login, char *pass );
>
> main()
> {
> MYSQL auth_server, *retv=NULL;
> char *login, *pass, buff[256];
>
> retv = mysql_connect( &auth_server, SERVER, SERVER_LOGIN, SERVER_PASS );
> mysql_select_db( &auth_server, "databaza" );
> setbuf( stdout, NULL );
>
> while( fgets( buff, sizeof(buff), stdin ) )
> {
> login = strtok( buff, " \n" );
> pass = strtok( NULL, " \n" );
>
> if( auth_ok( &auth_server, login, pass ) )
> puts("OK");
> else
> puts("ERR");
> }
> mysql_close( &auth_server );
> }
> --------------------------------------------
> naozaj si uz s tym neviem rady
Tak jsem to prosel a je potreba nahradit radek s mysql_connect
za funkci mysql_real_connect.
Viz manual.txt:
*mysql_connect()* Connects to a MySQL server. This function is
deprecated; use `mysql_real_connect()' instead.
To connect to the server, call `mysql_init()' to initialize a
connection handler, then call `mysql_real_connect()' with that handler
(along with other information such as the hostname, username, and
password). Upon connection, `mysql_real_connect()' sets the
`reconnect' flag (part of the MYSQL structure) to a value of `1'. This
flag indicates, in the event that a query cannot be performed because
of a lost connection, to try reconnecting to the server before giving
up. When you are done with the connection, call `mysql_close()' to
terminate it.
Takze ted to vypada nasledovne:
#include <string.h>
#include <stdio.h>
//#include <mysql/mysql.h>
#include <mysql.h>
#include <time.h>
#define SERVER "localhost"
#define SERVER_LOGIN "user"
#define SERVER_PASS "heslo"
main()
{
MYSQL auth_server, *retv=NULL;
char *login, *pass, buff[256];
mysql_init(&auth_server);
mysql_options(&auth_server,MYSQL_OPT_COMPRESS,0);
mysql_options(&auth_server,MYSQL_READ_DEFAULT_GROUP,"odbc");
if (!mysql_real_connect(&auth_server,SERVER, SERVER_LOGIN, SERVER_PASS,
"databaza",0,NULL,0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&auth_server));
}
else
{
setbuf( stdout, NULL );
while( fgets( buff, sizeof(buff), stdin ) )
{
login = strtok( buff, " \n" );
pass = strtok( NULL, " \n" );
if( auth_ok( &auth_server, login, pass ) )
puts("OK");
else
puts("ERR");
}
mysql_close( &auth_server );
}
}
Karel Dudasek
e-mail: Karel.Dudasek na vghur.army.cz Karel.Dudasek na dobruska.cz
Spravce poc. site VGHUr Mirova 727
tel: +420 973 257 874 Cs. Odboje 518 01 Dobruska
fax: +420 973 257 620 518 16 Dobruska tel: +420 777 623 639
Další informace o konferenci Linux