|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
Variable Interpolation
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:
Code:
db_query_function(“SELECT name FROM $table WHERE name=’$name’”); How do I do this in C? |
|
#2
|
|||
|
|||
|
is your question how do I use C to work with a mysql DB ?
|
|
#3
|
|||
|
|||
|
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. Another example: Code:
system("ls -l | $command");
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: Code:
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. |
|
#4
|
|||
|
|||
|
I know that "sprintf" do the trick but i'm not sure if it is considered C.
syntax: sprintf( char * szDest, const char *szFormat [, aruments]) for example: 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?) Gmorph. |
|
#5
|
|||
|
|||
|
Yeah, but thats for printing to stdout.
|
|
#6
|
|||
|
|||
|
That's printf. sprintf does the same thing but into the buffer that you supply.
|
|
#7
|
|||
|
|||
|
Doing database queries in C/C++ is not like it is in PHP/Perl because you can not simply:
PHP Code:
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. PHP Code:
If at another point you wanted to select data about a user, you'd use a function for that: PHP Code:
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). Last edited by Teaser : March 14th, 2003 at 11:33 PM. |
|
#8
|
|||
|
|||
|
Ok, I've read the man pages on sprintf() and all the other closly related functions. That's what I was looking for.
Thanks! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Variable Interpolation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|