List:General Discussion« Previous MessageNext Message »
From:Sasha Pachev Date:April 21 1999 8:23pm
Subject:Re: using an insert in the C API query function
View as plain text  
Tavaris Thomas wrote:
> To whom it may concern:
> I am running MySQL ver 3.22.20a
> My compiler is egcs 1.1.2 on a SUN Ultra 1 with Solaris 2.6
> I am trying to write CGIs to access MySQL via a web browser. (written in C)
> I am trying to pass a string into my mysql_query function call (using INSERT
> as the MySQL command)
> The mysql_query command (takes the MySQL command in quotes)
> My string is declared as  char *input;
> as is being passed into a function as a parameter.
> here is what I have:
>   // Send the query to the database
>    if (mysql_query(&mysql, "INSERT INTO test VALUES('input')") != 0) {
>       printf("Error sending query: %s\n", mysql_error(&mysql));
>       return (2);
> The above does not work because the input is in the quotes(ie its putting
> the word input into my table not the value of table)
> How do I pass the variable into an INSERT command?
> I couldn't find any examples in the client code.
> Any help is greatly appreciated.
> -Tavaris
> tavaris@stripped


 Here is how you do it:

 char query_buf[2048];
 char input[64];
 get_input_from_user(input); // initialize input here
.... do you mysql_connect and check for errors
 snprintf(query_buf, sizeof(query_buf), "insert into
test values ('%s')", input);
 mysql_query(&mysql, query_buf);

Sasha Pachev (home) (work)
using an insert in the C API query functionTavaris Thomas22 Apr
  • Re: using an insert in the C API query functionSasha Pachev22 Apr