User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:mysql:mysql.h:mysql_escape_string

This is an old revision of the document!


mysql_escape_string

description of mysql_escape_string

mysql_escape_string escape a mysql string
” will be escaped ; will not be escaped

#include <stdio.h> /* including standard library */ 
#include <mysql/mysql.h>
 
MYSQL *my;
MYSQL_RES *result;
int main( void ){
char host[20];
char user[20];
char pass[20];
 
// variables for escape
char from[90];
char query[90];
char to[90];
unsigned int length = 0;
 
my = mysql_init(NULL);
 
sprintf(host,"localhost");
sprintf(user,"username");
sprintf(pass,"password");
 
if (my == NULL ) {
printf("Cant initalisize MySQL\n");
return 1;
}
 
if( mysql_real_connect (my,host,user,pass,NULL,0,NULL,0)  == NULL) {
  printf("Error cant login\n");
  } else {
  printf("Login correct\n");
  }
 
 
mysql_select_db(my, "test");
 
// e.g. what the user send ... in this case a SQL injection
sprintf(from, "user1 ; SELECT * FROM usertable WHERE userid = \"user2\"");
 
 
length = strlen(from);
 
mysql_escape_string(to, from, length);
 
// new escaped string
sprintf(query, "SELECT id FROM usertable WHERE user_id = \"%s\"; ",to);
 
printf("%s\n",query);
 
mysql_close(my);
return 0;
}

output of mysql_escape_string c example

  Login correct
  SELECT id FROM usertable WHERE user_id = "user1 ; SELECT * FROM usertable WHERE userid = \"user2\"";

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/mysql/mysql.h/mysql_escape_string.1362010614.txt · Last modified: 2013/02/28 01:16 by 158.181.88.241

Impressum Datenschutz