#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Long Beach, California
    Posts
    86
    Rep Power
    13

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Belgium
    Posts
    60
    Rep Power
    12

    Question


    is your question how do I use C to work with a mysql DB ?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Long Beach, California
    Posts
    86
    Rep Power
    13
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    ISRAEL
    Posts
    35
    Rep Power
    13
    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Long Beach, California
    Posts
    86
    Rep Power
    13
    Yeah, but thats for printing to stdout.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    18
    That's printf. sprintf does the same thing but into the buffer that you supply.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    6
    Rep 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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    Long Beach, California
    Posts
    86
    Rep Power
    13
    Ok, I've read the man pages on sprintf() and all the other closly related functions. That's what I was looking for.

    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo