# If defined (_ WIN32) | defined (_ WIN64) // to support compilation on windows # Include <windows. h> # Endif # Include <stdio. h> # Include <stdlib. h> # Include "mysql. h" // on my machine, the file is in/usr/local/include/mysql. // Define the macros for database operations, or write them directly into the code without defining them. # Define SELECT_QUERY "select username from tbb_user where userid = % d" Int main (int argc, char ** argv) // char ** argv is equivalent to char * argv [] { MYSQL mysql, * sock; // defines the database connection handle, which is used by almost all MySQL functions MYSQL_RES * res; // query result set, structure type MYSQL_FIELD * fd; // structure containing field information MYSQL_ROW row; // string array that stores the query results of a row Char qbuf [160]; // stores the query SQL statement string If (argc! = 2) {// Check the input parameters Fprintf (stderr, "usage: mysql_select <userid> nn "); Exit (1 ); } Mysql_init (& mysql ); If (! (Sock = mysql_real_connect (& mysql, "localhost", "dbuser", "dbpwd", "9tmd_bbs_utf8", 0, NULL, 0 ))){ Fprintf (stderr, "Couldn't connect to engine! N % snn ", mysql_error (& mysql )); Perror (""); Exit (1 ); } Sprintf (qbuf, SELECT_QUERY, atoi (argv [1]); If (mysql_query (sock, qbuf )){ Fprintf (stderr, "Query failed (% s) n", mysql_error (sock )); Exit (1 ); } If (! (Res = mysql_store_result (sock ))){ Fprintf (stderr, "Couldn't get result from % sn", mysql_error (sock )); Exit (1 ); } Printf ("number of fields returned: % dn", mysql_num_fields (res )); While (row = mysql_fetch_row (res )){ Printf ("Ther userid # % d's username is: % sn", atoi (argv [1]), (row [0] = NULL )&&(! Strlen (row [0])? "NULL": row [0]); Puts ("query OK! N "); } Mysql_free_result (res ); Mysql_close (sock ); Exit (0 ); Return 0; // Add this line to ensure compatibility with most compilers } |