#include #include #include #include #define DB_DBNAME "testdb" #define DB_DBUSER "testuser" #define DB_DBPWD "testpassword" #define DB_DBHOST "localhost" #define DB_DBPORT 3306 int main() { /* this query returns all the rows successfully */ //char *query = "select PK, VALUE from TEST"; /* This query, with an input_buffer value of 1, does not return anything. */ /* The query log shows that the value of the bind variable is 196864, not the value of 1 */ /* that is in the buffer. The following shows the contents of the query log: */ /* 061127 21:47:31 28 Connect testuser@localhost on testdb */ /* 28 Prepare [1] select PK, VALUE from TEST where PK = ? */ /* 28 Execute [1] select PK, VALUE from TEST where PK = 196864 */ /* 28 Quit */ /* The query work correctly if I use the binary distribution of mysql instead of the */ /* version that I compiled myself: */ /* 061127 23:31:36 12 Connect testuser@localhost on testdb */ /* 12 Prepare [1] select PK, VALUE from TEST where PK = ? */ /* 12 Execute [1] select PK, VALUE from TEST where PK = 1 */ /* 12 Quit */ char *query = "select PK, VALUE from TEST where PK = ?"; MYSQL *mysql; MYSQL_STMT *stmt; MYSQL_BIND params[1]; int input_buffer; /* Setup prepared statement parameters */ memset(params, 0, sizeof(params)); params[0].buffer_type = MYSQL_TYPE_LONG; params[0].buffer = (int*) &input_buffer; input_buffer = 1; /*******************/ /* Result set data */ /*******************/ MYSQL_BIND col[2]; /* Result Set Buffers */ my_bool is_null[2]; unsigned long length[2]; int pk_buffer; int value_buffer; /* initialize mysql */ mysql = mysql_init((void *)NULL); if (!mysql) { printf("mysql_init(): %s\n", mysql_error(mysql)); return 1; } /* connect to the database */ if (mysql_real_connect(mysql, DB_DBHOST, DB_DBUSER, DB_DBPWD, DB_DBNAME, DB_DBPORT, NULL, 0) == NULL) { printf("mysql_real_connect(): %s\n", mysql_error(mysql)); return 1; } /* Create a prepared statement */ stmt = mysql_stmt_init(mysql); if (!stmt) { printf("mysql_stmt_init(): %s\n", mysql_error(mysql)); return 1; } /* Attach the query */ if (mysql_stmt_prepare(stmt, query, strlen(query))) { printf("mysql_stmt_prepare(): %s\n", mysql_error(mysql)); return 1; } /* Bind the input buffers */ if (mysql_stmt_bind_param(stmt, params)) { fprintf(stderr, " mysql_stmt_bind_param() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); return 1; } /* Execute the query */ if (mysql_stmt_execute(stmt)) { printf(" mysql_stmt_execute(), 1 failed\n"); printf(" %s\n", mysql_stmt_error(stmt)); return 1; } /* Setup the result buffers */ /* INTEGER COLUMN */ col[0].buffer_type= MYSQL_TYPE_LONG; col[0].buffer= (char *)&pk_buffer; col[0].is_null= &is_null[0]; col[0].length= &length[0]; /* INTEGER COLUMN */ col[1].buffer_type= MYSQL_TYPE_LONG; col[1].buffer= (char *)&value_buffer; col[1].is_null= &is_null[1]; col[1].length= &length[1]; /* Bind the result buffers */ if (mysql_stmt_bind_result(stmt, col)) { printf(" mysql_stmt_bind_result(), 1 failed\n"); printf(" %s\n", mysql_stmt_error(stmt)); return ; } /* fetch the results */ while (!mysql_stmt_fetch(stmt)) { printf("PK: %d, Value: %d\n",pk_buffer,value_buffer); } if (mysql_stmt_close(stmt)) { fprintf(stderr, " failed while closing the statement\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } mysql_close(mysql); return 0; }