Solved

PHP & MySQL

Posted on 2011-03-14
5
315 Views
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\mysite.com\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\mysite.com\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.
0
Comment
Question by:raulmonda
  • 2
  • 2
5 Comments
 
LVL 82

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.
0
 

Author Comment

by:raulmonda
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());

$name=mysql_real_escape_string($_POST[name1]);

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

Open in new window



if i replace the line 8 with
 $name="$_POST[name1]";

Open in new window

it works fine.

what i did wrong?
Regards,
Raul
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 350 total points
ID: 35132010
Well this

$name="$_POST[name1]";

should really be

$name=$_POST["name1"];

Similary

$name=mysql_real_escape_string($_POST[name1]);

should really be

$name=mysql_real_escape_string($_POST['name1']);

(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?

0
 

Author Closing Comment

by:raulmonda
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.

Regards,
Raul
0
 
LVL 82

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 33
Adding Extra Information box 4 26
Page showing diff display 4 22
MySQL: Updating SubQuery Match Faster 9 18
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now