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
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);
Related
I have an online database and what I need to do is to fetch data from it and save to my local database(localhost). I tried adding remote mysql database cause it said it is needed(I added the IP address of my local computer), I dont know if its right. The problem is it gives me an error
"arning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '10.24.168.85' (4) in /home/umalert/public_html/server/_includes/connection.php on line 18
Database connection failed:".
For now I have this connection script:
// create database connection
$connection_local = mysql_connect('x.x.x.x:3306', 'xxx', 'yyy');
if(!$connection_local) {
die ("Database connection failed: " . mysql_error());
}
// selece database to use
$db_select_local = mysql_select_db('umalert_db', $connection_local);
if(!$db_select_local) {
die("Database connection failed: ". mysql_error());
}
Hope you can help me with this. Thank you.
you should export your data from one database and import to another one.
I just started programming in php and would like to ask a question about the database selection code for mysql in the php coding.
I used phpmyadmin to create a database "admin" when in phpmyadmin I click on privileges and see the name as"admin#127.0.0.1". I created a connection to the database using this code in PHP:
<?php $connection = mysqli_connect("127.0.0.1", "admin", "admin123");
if (!$connection)
die("Database connection failed:" . mysqli_error());
and now i want to select the tables in the database I use this command:
$selected = mysqli_select_db("admin", $connection);
if (!$selected)
{
die('Database selection failed:' .mysqli_error());
}
?>
I know it connects because when only using the connection command when opening my broswer I can see the header i put in html, but I get an error with the selection command and cannot continue.
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\Program Files\EasyPHP-DevServer-13.1VC11\data\localweb\projects\databaZE.php on line 6##
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\Program Files\EasyPHP-DevServer-13.1VC11\data\localweb\projects\databaZE.php on line 8
Database selection failed:
Firstly is there a problem with the way I wrote my database name thats why it cannot connect and giving me error msg's?I used 127.0.0.1 as database, admin#127.0.0.1 but still same msg. I tried both mysql and mysqli but it doesnt seem to work also.
Edit: first time user sorry am a little confused with inputting code.
You have them the wrong way around,
$selected = mysqli_select_db("admin", $connection);
should be
$selected = mysqli_select_db($connection, "admin");
And
die('Database selection failed:' .mysqli_error());
should be
die('Database selection failed:' .mysqli_error($connection));
$mysqlServer = "***";
$mysqlDb = "***";
$mysqlUser = "***";
$mysqlPass = "***";
$conn = mysqli_connect($mysqlServer, $mysqlUser, $mysqlPass) or die("failed to connect to db");
mysqli_select_db($conn, $mysqlDb) or die("failed to connect select db");
i have this code, and its working without any problem. But if i try to input a wrong sql server or test it to perform an error. This will display:
Warning: mysqli_connect(): (HY000/2002): 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.
failed to connect select db
i don't want the warning to display if ever theres a problem in connecting the sql server. i just want my own error to display.
2 possible options:
set the error_reporing level to NOT to show warnings http://php.net/manual/en/function.error-reporting.php
put a # sign before mysqli_connect, this supresses the warning message
putting # sign before each function hide errors
$conn = #mysqli_connect($mysqlServer, $mysqlUser, $mysqlPass) or die("failed to connect to db");
Try this one:
$conn = mysqli_connect($mysqlServer, $mysqlUser, $mysqlPass, $mysqlDb);
Pass the DB name with the connect as fourth param.
<?php
// Connect to mysql server
$link = mysql_connect(HOST, USER, PASSWORD);
if(!$link) {
die('Could not connect to server: ' . mysql_error());
}
// Select database
$db = mysql_select_db(DATABASE);
if(!$db) {
die('Cannot use the database');
}
mysql_set_charset('charset=utf8', $link);
//code
?>
I am php beginer.I have written a simple php HTTP API which read some data from database and return in body. This API is accessed by many uses application asynchronously. When we increase the number of user more than 200 each second, i nam getting following warning
PHP Warning: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Too many connections in /home/ws/public_html/include/get_details.php on line 3
I want to know best solution for it.
Thanks
first you can use mysqltuner to check your db status. Other solution, use redis or memcache to reduce the connections. If you have your sessions stored in DB, try to change and use redis.phpredis
I am trying to insert data to a web site from the code of another web site. How can I use the mysql_connect() command for that. Can I use the IP address of the first web site in the second web site code?
Please help me
Yes, you can use a remote MySQL server with mysql_connect(). Use mysql_connect() according to the PHP manual (http://php.net/manual/de/function.mysql-connect.php) and use your server's IP as $server. However, make sure the MySQL port is accessible from remote on the MySQL server (port 3306).
For inserting data in the database you need to connect the database
Selecting DATABASE
you need to just connect the server on which the db exist in which we are inserting
<?php
$link = mysql_connect('ip or server of the db in which inserting', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make myDB the current database
$db_selected = mysql_select_db('myDB', $link);
if (!$db_selected) {
die ('Can\'t use myDB: ' . mysql_error());
}
?>
for inserting records in another website , you need to just connect the server and DB of that particular website, and fire Queries.
let me know if more..