orange08 Posted July 30, 2009 Share Posted July 30, 2009 my code work fine, until i add mysql_real_escape_string() for my user inputs and get all the warning like this: Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'apache'@'localhost' (using password: NO) in /home/mysite/domains/mysite.com/public_html/phpm/mypage.php on line 157 line 157 is the line that i add mysql_real_escape_string() for my user input... so, what error i have made here? Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/ Share on other sites More sharing options...
phpSensei Posted July 30, 2009 Share Posted July 30, 2009 mysql_real_escape_string() takes a connection handler and escapes the string according to the current character set. Although depreciated, mysql_escape_string doesn't need a connection. You must connect to your mysql before using thisl Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/#findComment-886688 Share on other sites More sharing options...
orange08 Posted July 30, 2009 Author Share Posted July 30, 2009 mysql_real_escape_string() takes a connection handler and escapes the string according to the current character set. Although depreciated, mysql_escape_string doesn't need a connection. You must connect to your mysql before using thisl ya, thanks. the problem is solved now... as a newbie, i would like to know can i just put the mysql connection code at the top of each php file regardless it's needed or not, to avoid the same problem happen again? and need i to explicitly close the connection each time it's used? Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/#findComment-886711 Share on other sites More sharing options...
phpSensei Posted July 30, 2009 Share Posted July 30, 2009 mysql_real_escape_string() takes a connection handler and escapes the string according to the current character set. Although depreciated, mysql_escape_string doesn't need a connection. You must connect to your mysql before using thisl ya, thanks. the problem is solved now... as a newbie, i would like to know can i just put the mysql connection code at the top of each php file regardless it's needed or not, to avoid the same problem happen again? and need i to explicitly close the connection each time it's used? There is no implementation to PHP to make that happen automatically. It needs a connection handler, meaning its required everytime you use it in a php script. To free more memory you can close the connection. I believe making a file called "dbconnection.php" and placing it in your include folders or wherever you like is helpful. on top of every page just put include("dbconnection.php"); Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/#findComment-886713 Share on other sites More sharing options...
orange08 Posted July 30, 2009 Author Share Posted July 30, 2009 There is no implementation to PHP to make that happen automatically. It needs a connection handler, meaning its required everytime you use it in a php script. what do you meant in this case, please? mysql_real_escape_string()? To free more memory you can close the connection. I believe making a file called "dbconnection.php" and placing it in your include folders or wherever you like is helpful. on top of every page just put include("dbconnection.php"); ya, currently i did do like what exactly you have suggested here. but, i didn't have any code to close the connection...so, seem that i kept include the dbconnection.php in each of my php file...will this causing any problem, when many visitors visit my site and open every page again and again, making the connection kept established? Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/#findComment-886727 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 The mysql connection will be closed always after the script execution ends. So no it is not a must to close it. Unless you want to close it before the script executions has come to an end. Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/#findComment-886738 Share on other sites More sharing options...
phpSensei Posted July 30, 2009 Share Posted July 30, 2009 There is no implementation to PHP to make that happen automatically. It needs a connection handler, meaning its required everytime you use it in a php script. what do you meant in this case, please? mysql_real_escape_string()? To free more memory you can close the connection. I believe making a file called "dbconnection.php" and placing it in your include folders or wherever you like is helpful. on top of every page just put include("dbconnection.php"); ya, currently i did do like what exactly you have suggested here. but, i didn't have any code to close the connection...so, seem that i kept include the dbconnection.php in each of my php file...will this causing any problem, when many visitors visit my site and open every page again and again, making the connection kept established? If you want to close it during the script.... then use mysql_close. Otherwise non-persistent open links are automatically closed at the end of the script's execution. The mysql_connect is a resource, you wont overwrite the last connection, it doesnt RE-establish, it only does it once during the script everytime its loaded. no need to worry. Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/#findComment-886740 Share on other sites More sharing options...
orange08 Posted July 30, 2009 Author Share Posted July 30, 2009 There is no implementation to PHP to make that happen automatically. It needs a connection handler, meaning its required everytime you use it in a php script. what do you meant in this case, please? mysql_real_escape_string()? To free more memory you can close the connection. I believe making a file called "dbconnection.php" and placing it in your include folders or wherever you like is helpful. on top of every page just put include("dbconnection.php"); ya, currently i did do like what exactly you have suggested here. but, i didn't have any code to close the connection...so, seem that i kept include the dbconnection.php in each of my php file...will this causing any problem, when many visitors visit my site and open every page again and again, making the connection kept established? If you want to close it during the script.... then use mysql_close. Otherwise non-persistent open links are automatically closed at the end of the script's execution. The mysql_connect is a resource, you wont overwrite the last connection, it doesnt RE-establish, it only does it once during the script everytime its loaded. no need to worry. then, will it causing many connections being open due to increase of visitors, and at the end causing memory leak? Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/#findComment-886744 Share on other sites More sharing options...
phpSensei Posted July 30, 2009 Share Posted July 30, 2009 There is no implementation to PHP to make that happen automatically. It needs a connection handler, meaning its required everytime you use it in a php script. what do you meant in this case, please? mysql_real_escape_string()? To free more memory you can close the connection. I believe making a file called "dbconnection.php" and placing it in your include folders or wherever you like is helpful. on top of every page just put include("dbconnection.php"); ya, currently i did do like what exactly you have suggested here. but, i didn't have any code to close the connection...so, seem that i kept include the dbconnection.php in each of my php file...will this causing any problem, when many visitors visit my site and open every page again and again, making the connection kept established? If you want to close it during the script.... then use mysql_close. Otherwise non-persistent open links are automatically closed at the end of the script's execution. The mysql_connect is a resource, you wont overwrite the last connection, it doesnt RE-establish, it only does it once during the script everytime its loaded. no need to worry. then, will it causing many connections being open due to increase of visitors, and at the end causing memory leak? like i said it closes itself at the end of the script, ALL OF THE CONNECTIONS WILL CLOSE! If theres any reason you want to speed up the script then close it right after your done with the connection. Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/#findComment-886750 Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 I am quite sure this won't be and will not ever be the bottleneck of your application. There will be sure some other things that will be slowing down much more than not closed mysql connection so I wouldn't worry about it. Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/#findComment-886755 Share on other sites More sharing options...
orange08 Posted July 30, 2009 Author Share Posted July 30, 2009 ok, thanks guys for the explanation! Quote Link to comment https://forums.phpfreaks.com/topic/168122-mysql_real_escape_string/#findComment-886759 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.