$php_mysql$ Posted July 10, 2011 Share Posted July 10, 2011 hey guys im trying to clean my strings with this function ///////////////////////////////////Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); /*$str = htmlspecialchars($str);*/ } return mysql_real_escape_string($str); } my script is connected to database and inserts data in field too but using the clean function im getting this error Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/ Share on other sites More sharing options...
$php_mysql$ Posted July 10, 2011 Author Share Posted July 10, 2011 i get the error while submiting the form Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240857 Share on other sites More sharing options...
QuickOldCar Posted July 10, 2011 Share Posted July 10, 2011 Are you connected to a database before using this function? Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240859 Share on other sites More sharing options...
$php_mysql$ Posted July 10, 2011 Author Share Posted July 10, 2011 yes see this is my include page <?php session_start(); error_reporting(E_ALL & ~E_NOTICE); include 'config.php'; include 'functions/dbfunctions.inc.php'; include 'functions/gnrlfunctions.inc.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240875 Share on other sites More sharing options...
phpmady Posted July 10, 2011 Share Posted July 10, 2011 Hi, Your code is not connected with db, Please check it, or post your include script thanks Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240877 Share on other sites More sharing options...
$php_mysql$ Posted July 10, 2011 Author Share Posted July 10, 2011 here is my include page <?php session_start(); error_reporting(E_ALL & ~E_NOTICE); include 'config.php'; include 'functions/db_functions.inc.php'; include 'functions/gn_functions.inc.php'; ?> my config page <?php // Database information $host = 'localhost'; $user = 'root'; $password = 'demo'; $database ='abcd'; ?> my db_functions.inc.php <?php //////////////////////database conntction//////////////// function db_connect() { global $host; global $user; global $password; global $database; if(!$conn = mysql_connect($host, $user, $password)) { die("Could't connect to database server.....".mysql_error()); } else { mysql_select_db($database, $conn) or die(mysql_error()); return $conn; } } /////////////////////////////////////SQL Execution function executeSql($sql){ $conn = db_connect(); $result = mysql_query($sql, $conn) or die(mysql_error($conn)); return $result; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240880 Share on other sites More sharing options...
phpmady Posted July 10, 2011 Share Posted July 10, 2011 hi, It seems u are missing the function call? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240885 Share on other sites More sharing options...
$php_mysql$ Posted July 10, 2011 Author Share Posted July 10, 2011 im sorry i didn get u mate, where am i missing what? Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240924 Share on other sites More sharing options...
$php_mysql$ Posted July 10, 2011 Author Share Posted July 10, 2011 any help in here? Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240967 Share on other sites More sharing options...
spiderwell Posted July 10, 2011 Share Posted July 10, 2011 A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated, and FALSE is returned. from here: http://php.net/manual/en/function.mysql-real-escape-string.php this is probably why you are getting an error Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240979 Share on other sites More sharing options...
$php_mysql$ Posted July 10, 2011 Author Share Posted July 10, 2011 but if you look at my include page i put first config the login and tables info, then i put db connection function and after that i included general functions and that is where the mysql_real_escape_string is so dont you think i have already connected to db? Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240983 Share on other sites More sharing options...
Alex Posted July 10, 2011 Share Posted July 10, 2011 As far as I can see, you're never calling db_connect(). Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240984 Share on other sites More sharing options...
$php_mysql$ Posted July 10, 2011 Author Share Posted July 10, 2011 oops :-) it tool me a whole day and i didn realize i wasnt even calling the function. thanks guys and Alex thanks for pointing me the right way. Quote Link to comment https://forums.phpfreaks.com/topic/241581-function-giving-eror/#findComment-1240986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.