#include <mysql.h>
#include <stdio.h>
int main()
{
    MYSQL_RES *result;
    MYSQL_ROW row;
    MYSQL *connection, mysql;
    char *query="select test_id, test_val from test";
    int state;

    //초기화
    mysql_init(&mysql);

    //연결
    connection = mysql_real_connect(&mysql, "localhost",
    "root", "pw", "db_test",0, 0, 0);
    if(connection == NULL)
    {
        printf(mysql_error(&mysql));
    }

    //질의
    state = mysql_query(connection, query);
    if(state != 0)
    {
        printf(mysql_error(connection));
    }

    //결과추출
    result = mysql_store_result(connection);
    printf("rows: %dn", mysql_num_rows(result));
    while( (row=mysql_fetch_row(result)) != NULL)
    {
        printf("id:%s, pw:%sn",
        (row[0]?row[0]:"NULL"), (row[1]?row[1]:"NULL"));
    }

    //끝장
    mysql_free_result(result);
    mysql_close(connection);
 
    return 0;
}
Posted by 용학도리
,