Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an IP address that displays on screen fine. I need to grab the value of that and include it in a SQL statement how would I do that?

C++
// Checks the protocol IP is encapsulating
     memset( ipSrc, 0x00, sizeof(ipSrc) );
     memset( ipDest, 0x00, sizeof(ipDest) );
     translate_ip(ip_header->source_ip, ipSrc);
     translate_ip(ip_header->destination_ip, ipDest);
     char ipSrc[20]
     // Read http://www.ietf.org/rfc/rfc1700.txt?number=1700
     if ( bShowTCP )
       {
       printf("\n -------------------- // -------------------- ");
       printf("\n TCP Header:");

       int *addressValue = new int();
       char *address = LIP;
       inet_pton(AF_INET, address, addressValue);
         if (ip_header->source_ip == *addressValue)
         {
         printf("\n   Source      IP: %s", "0.0.0.0");
         printf("\n   Destination IP: %s", ipDest);
         }
         else
         {
         printf("\n   Source      IP: %s", ipSrc);
         printf("\n   Destination IP: %s", ipDest);
         (mysql_real_connect(conn,"urlock.db.5513143.hostedresource.com","urlock","Admin1234","urlock",0,NULL,0) !=0);
         (mysql_query(conn,"SELECT COUNT(*) FROM tblURLIP WHERE IP = 'ipSrc' And IPStatus = '1' And IPMax = '0'"));
         my_ulonglong i = 0;
         res_set = mysql_store_result(conn);
         my_ulonglong numrows = mysql_num_rows(res_set);
         LEGIT = mysql_fetch_row(res_set);
Posted

Don't suppose you're comming from php?

char quaeryString[1024];

sprintf(queryString, "SELECT COUNT(*) FROM tblURLIP WHERE IP = '%s' AND IPStatus = '1' AND IPMax = '0'", ipSrc);

...
...
...

mysql_query(conn, queryString);

....
....
 
Share this answer
 
Comments
Member 7766180 6-Aug-11 10:46am    
No Im not from PHP. Thank you for your post. A question, is this in C++? I'm new to all of this. I have another solution posted that looks sorta like this and I was told that its in C....I'm a little confused. The other one works in my C++ code but I would like to stay in C++, any info appreciated. Thanks!
Member 7766180 6-Aug-11 10:57am    
This works great! Only one question is this C++?
:grins: - I just ask because the code looks very much like php code for the same task..

Well, C++ is simply a superset of C - that is to say if it's in C, it's in C++ (by and large)

Given that you're using printf(C) rather cout(C++) for output, I'd assumed that the solution would be acceptable. To be honest, I don't know off-hand how to do this without using any sprintf. I would suppose that this (or something quite similar) would do the trick:

string queryString;

queryString = "SELECT COUNT(*) FROM tblURLIP WHERE IP = '";
queryString += ipSrc;
queryString += "' AND IPStatus = '1' AND IPMax = '0'"

mysql_query(conn, queryStr.c_str());


Unless the MYSQL functions have been wrapped up inside a class, they're also actually C functions (note that I have to ask the string class to give me a C-style string that I can give to the mysql_query function)
Given also that the mysql functions want a C style string, it seems a shameful waste to convert C style strings to a C++ string object, before converting back to a c style string, ready to be hand-balled off to mysl_query - all simply so that you can insert the string containing the IP address into your query string.

You can'd add multiple strings together in a single line to create a C++ string
This won't work
string myString;
char *myName = "enhzflep"
myString = "Hello " + myName + " welcome to World";


The C++ functional equivalent would be:
string myString;
char *myName = "enhzflep";
myString = "Hello ";
myString += myName;
myString += " welcome to World";




While in C, using sprintf this string concatenation CAN be done in a single-swoop.
char *myName = "enhzflep";
const int bufferSize = 1024;
char myString[bufferSize];
sprintf(myString, "Hello %s welcome to World", myName);

However, you will note that (a) I have to manage the memory used for the queryString and that (b) my code makes no check that the computed string will in fact fit into the 1024 byte buffer I've allowed for this.

C++ strings automatically manage their own memory, so you wont run into this kind of danger - though one does still need to rigorous in checking that any user-entered text that gets inserted into an SQL statement is first checked for validity.


A prime example of buffer overrun may be found in one of ID Software's old games. In quake, there is some code that checks for the available graphics card etensions. At the time, 1024 byte buffer to store the name of all of these extensions seemed prudent to John Carmack. With time the number of extension supported grew (There are now several hundred, often with 10-15 byte long names), obviously, there came a time when all of the names would no longer fit into this 1024 byte buffer.
Here's the real kicker: You wanna know what the fix was (for what was by then no longer brand-new, and long before the days of internet-based game-updates)

THE WRITERS OF (video-card) GRAPHICS DRIVERS CHECKED TO SEE IF THE CODE RUNNING THE glGetSupportedExtensions (or whatever the exact function name is, I forget) WAS QUAKE. IF SO, THEY RETURNED A TRUNCATED STRING - A hack inside of graphics drivers to allow for a clumsy mistake in an already released product!!
 
Share this answer
 
Comments
Member 7766180 6-Aug-11 14:05pm    
This answer deserves a 20! Thank you so much for the information! It is much appreciated!
enhzflep 6-Aug-11 21:54pm    
It truly is a pleasure to help when it's not taken for granted.

I stand where I do today thanks to the generosity of those that allowed me to stand on their shoulders.

Well, that and curiosity that WOULD kill the cat :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900