Posted on 2011-03-14
Last Modified: 2012-08-14
hi experts,

Im having the following 2 errors when im trying to use the mysql_real_escape_string() function.

mysql_real_escape_string() [<a href='function.mysql-real-escape-string'>function.mysql-real-escape-string</a>]: Can't connect to MySQL server on 'localhost' (10061) in C:\CustomerData\webspaces\webspace_xxxxxxx\wwwroot\\some.php on line 23

Open in new window

 mysql_real_escape_string() [<a href='function.mysql-real-escape-string'>function.mysql-real-escape-string</a>]: A link to the server could not be established in C:\CustomerData\webspaces\webspace_xxxxxxx\wwwroot\\some.php on line 23

Open in new window

when im using INSERT INTO without thats function everything is fine.

To connect to the database i use the following code:

mysql_connect("localhost", "my_user", "my_password") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());

Open in new window

Any help in this matter will be highly appreciated.
Question by:raulmonda
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 150 total points
ID: 35131763
You do have to connect to the database before you use mysql_real_escape_string().  It is actually done by the MySQL server.

Author Comment

ID: 35131936
yes, i connect to the database before i use this function.

actually my code is :

$conn = mysql_connect("localhost", "my_user", "my_password") or die(mysql_error());
if (!$conn)
  die('Could not connect: ' . mysql_error());
mysql_select_db("my_database", $conn) or die(mysql_error());


 $sql="INSERT INTO users (name, lastname, email) VALUES ('$name', '$lastname1', '$email')";

Open in new window

if i replace the line 8 with

Open in new window

it works fine.

what i did wrong?
LVL 34

Accepted Solution

Beverley Portlock earned 350 total points
ID: 35132010
Well this


should really be




should really be


(note the ' and ' around name1.)

Other than that I doe not see anything really obvious. On your mysql_query that does the INSERT INTO add a DIE clause for testing

$sql="INSERT INTO users (name, lastname, email) VALUES ('$name', '$lastname1', '$email')";
mysql_query( $sql ) or die("Testing error - " . mysql_error() );

Open in new window

What shows up?


Author Closing Comment

ID: 35132064
well, thank you both.

My mistake. I used mysql_real_escape_string() BEFORE connecting. Newbie mistake. i just moved the code lines AFTER connecting and it work fine.

Thank you.

LVL 83

Expert Comment

by:Dave Baldwin
ID: 35132226
Cool, glad you got it working.  Thanks for the points.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question