Can't connect to MySQL server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tristanhall
Forum Newbie
Posts: 11
Joined: Sun Feb 07, 2010 6:12 pm

Can't connect to MySQL server

Post by tristanhall »

I am creating a PHP form that adds a record to a MySQL table. I have created the form and the PHP page that sends the data to the MySQL database. However whenever I submit the form I get the following error:
Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
The url for the form is http://www.lifegoesboom.com/open/index. ... =AddPerson. This is the code for the page that submits the data to the database:

Code: Select all

<?php
include "/open/mysql.php";
include "/open/functions/openconnection.php";
 
MYSQL_QUERY ("INSERT INTO `person` (`id`, `title`, `firstname`, `lastname`, `address1`, `address2`, `city`, `state`, `zipcode`, `homephone`, `workphone`, `mobilephone`, `emailaddress`, `spouse`, `child1`, `child2`, `child3`, `child4`, `child5`, `notes`, `amount`, `payments`) 
VALUES
('$_POST[title]','$_POST[firstname]','$_POST[lastname]','$_POST[address1]','$_POST[address2]','$_POST[city]','$_POST[state]','$_POST[zipcode]','$_POST[hoemphone]','$_POST[workphone]','$_POST[mobilephone]','$_POST[emailaddress]','$_POST[spouse]','$_POST[child1]','$_POST[child2]','$_POST[child3]','$_POST[child4]','$_POST[child5]','$_POST[notes]','$_POST[amount]','$_POST[payments]')");
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
 
include ('/open/functions/closeconnection.php');
?>
I am a newbie to PHP but I will do my best to understand any help provided. Any and all help is greatly appreciated.
Thank you!
-Tristan
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: Can't connect to MySQL server

Post by rahulzatakia »

Hi,
You don't have problem in this file. You have problem in which you have code for connecting database. So check out the file and please check all the paths you have given are correct. If still you have problem then just give the code of connection file.
tristanhall
Forum Newbie
Posts: 11
Joined: Sun Feb 07, 2010 6:12 pm

Re: Can't connect to MySQL server

Post by tristanhall »

I followed the instructions that my web host provided and I modded a few variables and this is what I got. The problem is still ocurring.

Code: Select all

<?php
MYSQL_CONNECT($server, $user, $password) or die ( "<H3>Server unreachable</H3>");
          MYSQL_SELECT_DB($database) or die ( "<H3>Database non existent</H3>");
?>
tabutcher
Forum Newbie
Posts: 20
Joined: Fri Aug 21, 2009 7:10 am

Re: Can't connect to MySQL server

Post by tabutcher »

hi,
I think you need to assign the function to a variable for example:

Code: Select all

 
resourceId = mysql_connect(server, username, password);
 
and when you are selecting a database the parameters to pass are the database name and the resource returned from the above connection function.

Code: Select all

 
mysql_select_db(databaseName, resourceId);
 
hope this leads you in the right direction.
tristanhall
Forum Newbie
Posts: 11
Joined: Sun Feb 07, 2010 6:12 pm

Re: Can't connect to MySQL server

Post by tristanhall »

I'm still having the same error message. I'm not sure if I did what you said correctly though. Here's what I put together:

Code: Select all

<?php
$con = MYSQL_CONNECT($server, $user, $password);
MYSQL_CONNECT($server, $user, $password) or die ( "<H3>Server unreachable</H3>");
          MYSQL_SELECT_DB($database,$con) or die ( "<H3>Database non existent</H3>");
?>
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: Can't connect to MySQL server

Post by rahulzatakia »

Hi tristanhall, please use the following code,

<?php
$con = MYSQL_CONNECT($server, $user, $password);
MYSQL_SELECT_DB($database_name,$con) or die ( "<H3>Database non existent</H3>");
?>
tristanhall
Forum Newbie
Posts: 11
Joined: Sun Feb 07, 2010 6:12 pm

Re: Can't connect to MySQL server

Post by tristanhall »

Thank you for all the help everyone! I have decided to use Dreamweaver and reverse engineer its code.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Can't connect to MySQL server

Post by klevis miho »

Do the $server, $user and $password variables have a value?
Also try to lowercase the functions.
timon_sun
Forum Newbie
Posts: 1
Joined: Mon Feb 08, 2010 2:22 am

Re: Can't connect to MySQL server

Post by timon_sun »

I think you should check out your firewall configuration, or you also could check your file and folder privilege on linux.

Good Luck!
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: Can't connect to MySQL server

Post by rahulzatakia »

Hi klevis miho,
$server, $user, $password has the values...

$server = hostname
$user = database user name
$password = password of the database
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Can't connect to MySQL server

Post by klevis miho »

try to lowercase the function names
Post Reply