MySQL Forums
Forum List  »  Connector/C (libmysql)

Need x86 prepared statement example
Posted by: Walter Oney
Date: May 11, 2020 11:04PM

I need to find a simple example of an x86 (32-bit) program using the 6.1 connector with a string parameter and a string result. In PHP, the following works (error checks omitted for clarity):

$stmt = mysqli_stmt_init(<database object>);
mysqli_stmt_prepare($stmt, "select textfield from database where key=?");
mysqli_stmt_bind_param($stmt, "s", $stringval);
mysqli_stmt_bind_result($stmt, $textvar);

After three days of trying and looking online, I can't duplicate this functionality using the C-API. I think my problem relates to correctly initializing the MYSQL_BIND structures, but none of the several samples I've found online seems to work for me. E.g.:

char keyval[] = "sample";
int strl_len = 256;
char strval[256];
MYSQL* h = mysql_init(NULL);
MYSQL_STMT* stmt = mysql_stmt_init(h);
char* query = "select textfield from database where key=?";
mysql_stmt_prepare($stmt, query, strlen(query));

MYSQL_BIND pbind, qbind;
memset(&pbind, 0, sizeof(pbind));
pbind.buffer = keyval;
pbind.buffer_length = strlen(keyval) + 1;
pbind.buffer_type = MYSQL_TYPE_STRING;
pbind.error = etc.;
pbind.length = &str_len;

mysql_stmt_bind_param(stmt, &pdind);
// mysql.general_log shows execution of select textfield from database
// where keyval=NULL

memset(&qbind, 0, sizeof(qbind);
qbind.buffer = strval;
qbind.buffer_length = 256;
qbind.buffer_type = MYSQL_TYPE_STRING;
qbind.length = &str_len;

mysql_stmt_bind_result($stmt, &qbind);

All API calls before fetch succeed. Fetch returns 257, which means "no more rows", even for the first row of the result set.

Options: ReplyQuote

Written By
Need x86 prepared statement example
May 11, 2020 11:04PM

Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.