-4

I have created a HTML form and im trying to link it to a PHP myadmin database.But it says

Warning: mysql_connect(): A connection attempt failed because the connected party did not

properly respond after a period of time, or established connection failed because

connected host has failed to respond. in C:\xampp\htdocs\form.php on line 8 Could not

connect:A connection attempt failed because the connected party did not properly respond

after a period of time, or established connection failed because connected host has failed

to respond.I dont know why thats happening.This is my code below,

<?php

    define('DB_NAME','Registernewaccount');   
    define ('DB_USER' ,'root'); 
    define('DB_PASSWORD','12345'); 
    define('DB_HOST','localhost');  
    $link = mysql_connect('DB_HOST','DB_USER','DB_PASSWORD'); 

    if (!$link){  

        die('Could not connect:' . mysql_error());      
    } 

   $db_selected = mysql_select_db(DB_NAME,$link);        

   if(!$db_selected){         

       die('Can\'t use' .DB_NAME .':' . mysql_error());        
    }     

    mysql_close(); 

?>     

Can you please help me

cwiggo
  • 2,541
  • 9
  • 44
  • 87
user1948682
  • 59
  • 1
  • 7
  • 4
    Haven't seen too many HTML forms which connect to a database. – alex Jan 04 '13 at 13:33
  • 1
    constants mustn't be wrapped in quotes – nanobash Jan 04 '13 at 13:34
  • Why not try to use the command line tool (see http://dev.mysql.com/doc/refman/5.5/en/mysql.html) on the machine in question to check that the username, password and hostname combination is correct. If not get that fixed. – Ed Heal Jan 04 '13 at 13:37
  • 1
    [DO **NOT** USE MYSQL_*](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Pranav 웃 Jan 04 '13 at 13:40
  • [Please, don't use mysql_* functions in new code.](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-function-in-php) They are no longer maintained and the [deprecation process](http://news.php.net/php.internals/53799) has begun on it. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://uk3.php.net/pdo) or [MySQLi](http://uk1.php.net/mysqli) - this [article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – ajtrichards Jan 04 '13 at 13:50

1 Answers1

3

Considering the fact i know nothing about your setup. And assuming you have a database server up and running. It's probably that you have quoted the constants in the mysql_connect call

Change

$link = mysql_connect('DB_HOST','DB_USER','DB_PASSWORD');

to

$link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
tlenss
  • 2,609
  • 2
  • 22
  • 26
  • [DO **NOT** USE MYSQL_*](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Pranav 웃 Jan 04 '13 at 13:39
  • Hi, Thankyou very much for your reply.Yes, I already created a database in Php my admin and the html form basically has fields such as name, account number , address and i want that information to be saved in a database so new customers are added to the company and their info is saved in a database.I tried removing the quotes and then it gave me this error – user1948682 Jan 04 '13 at 13:51
  • Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\form.php on line 9 Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\form.php on line 9 Could not connect:php_network_getaddresses: getaddrinfo failed: No such host is known. – user1948682 Jan 04 '13 at 13:52