C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old March 14th, 2003, 03:50 PM
Epsilon Epsilon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: Long Beach, California
Posts: 86 Epsilon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 10 m 34 sec
Reputation Power: 7
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?

Reply With Quote
  #2  
Old March 14th, 2003, 05:08 PM
Jonas_G Jonas_G is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Belgium
Posts: 60 Jonas_G User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Question

is your question how do I use C to work with a mysql DB ?

Reply With Quote
  #3  
Old March 14th, 2003, 07:45 PM
Epsilon Epsilon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: Long Beach, California
Posts: 86 Epsilon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 10 m 34 sec
Reputation Power: 7
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.

Reply With Quote
  #4  
Old March 14th, 2003, 08:21 PM
Gmorphus Gmorphus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: ISRAEL
Posts: 35 Gmorphus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to Gmorphus
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.

Reply With Quote
  #5  
Old March 14th, 2003, 08:35 PM
Epsilon Epsilon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: Long Beach, California
Posts: 86 Epsilon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 10 m 34 sec
Reputation Power: 7
Yeah, but thats for printing to stdout.

Reply With Quote
  #6  
Old March 14th, 2003, 11:02 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 19 h 1 m 34 sec
Reputation Power: 12
That's printf. sprintf does the same thing but into the buffer that you supply.

Reply With Quote
  #7  
Old March 14th, 2003, 11:31 PM
Teaser Teaser is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 6 Teaser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Doing database queries in C/C++ is not like it is in PHP/Perl because you can not simply:
PHP Code:
 query ("select $x, $y from blah where balh = '$a'"); 

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:
 int login (char usernamechar passsword) ;
/* returns 1 on successful login*/ 

If at another point you wanted to select data about a user, you'd use a function for that:
PHP Code:
 char ** getUserInfo (unsigned int id) ; 

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.

Reply With Quote
  #8  
Old March 15th, 2003, 01:34 AM
Epsilon Epsilon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: Long Beach, California
Posts: 86 Epsilon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 10 m 34 sec
Reputation Power: 7
Ok, I've read the man pages on sprintf() and all the other closly related functions. That's what I was looking for.

Thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Variable Interpolation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway