March 14th, 2003, 03:50 PM
How do I achieve variable interpolation in C? For example, if I wanted to SELECT a record from a database where the values are decided by variables in PHP, I would do this:
How do I do this in C?
db_query_function(“SELECT name FROM $table WHERE name=’$name’”);
March 14th, 2003, 05:08 PM
is your question how do I use C to work with a mysql DB ?
March 14th, 2003, 07:45 PM
No. I thought I made it pretty clear. I guess not.
What I wan't to do is use variable interpolation, like you would in Perl or PHP, in C. If I took out the $ in the above and put that in a C program, it wouldn't substitute the variables values for the input. It would look in table for name as name.
That's valid in PHP. In PHP, $ signals that what follows is a variable. There's nothing like that in C, so if I did:
system("ls -l | $command");
and 'command' was a variable, nothing is telling C that, and it will try to execute 'command' as the actual command.
system("ls -l | command");
March 14th, 2003, 08:21 PM
I know that "sprintf" do the trick but i'm not sure if it is considered C.
sprintf( char * szDest, const char *szFormat [, aruments])
sprintf ( szDest, "%sshed forum: id = %u", "dev", 7);
than szDest = "devshed forum: id = 7";
each %s is substituted with a string, and %u with a number
(there are many more types of course...)
"Gravitation can NOT be responsible for people falling in Love"
(one of the most significant characters in the history, can you guess?)
March 14th, 2003, 08:35 PM
Yeah, but thats for printing to stdout.
March 14th, 2003, 11:02 PM
That's printf. sprintf does the same thing but into the buffer that you supply.
March 14th, 2003, 11:31 PM
Doing database queries in C/C++ is not like it is in PHP/Perl because you can not simply:
Instead, you should provide a function for each task/query you wish to perform on the database. For example, you are likely to have a login part, in which case the login () function would do the query that logs a user in, and only that.
query ("select $x, $y from blah where balh = '$a'");
If at another point you wanted to select data about a user, you'd use a function for that:
int login (char * username, char * passsword) ;
/* returns 1 on successful login*/
so this function would get query the database based on a user id, and return the fields as an array of strings (or however you wanted to do it).
char ** getUserInfo (unsigned int id) ;
Last edited by Teaser; March 14th, 2003 at 11:33 PM.
March 15th, 2003, 01:34 AM
Ok, I've read the man pages on sprintf() and all the other closly related functions. That's what I was looking for.