1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| MYSQL *
do_mysql_connect (char *host_name, char *user_name, char *password,
char *db_name, unsigned int port_num, char *socket_name,
unsigned int flags)
{
MYSQL *conn; /* pointer to connection handler */
conn = mysql_init (NULL); /* allocate, initialize connection handler */
if (conn == NULL)
{
fprintf (stderr, "mysql_init() failed\n");
return (NULL);
}
if (mysql_real_connect (conn, host_name, user_name, password,
db_name, port_num, socket_name, flags) == NULL)
{
fprintf (stderr, "mysql_real_connect() failed:\nError %u (%s)\n",
mysql_errno (conn), mysql_error (conn));
return (NULL);
}
return (conn); /* connection is established */
}
int
main ()
{
char hostname[] = "my_host.com";
char user_name[] = "my_uname";
char password[] = "my_pass";
char db_name[] = "my_db";
unsigned int port = 64000;
char query[1024];
MYSQL *conn = NULL;
conn = do_mysql_connect (hostname, user_name, password, db_name,
port, NULL, 0);
mysql_query (conn, "select id from userdb");
snprintf (query, sizeof (query), "%s '%s'",
"UPDATE blahblah SET id = 2,lastcheck = now() WHERE ip =",
"127.0.0.1");
mysql_query (conn, query);
mysql_close (conn);
} |
Partager