<?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
Related
I am trying to connect to two different databses using php
error_reporting(E_ALL);
$con= mysqli_connect("localhost", "phpapp", "phpapp", "hazard") or die("error connecting database 1".mysqli_error($con));
$con_vpn= mysqli_connect("xxx.xxx.xxx.xxx", "user", "pass", "db_name") or die("error connecting database 2".mysqli_error($con_vpn));
When I run the application it is showing error : error connecting database 2. It is not even printing the error.
thanks in advance:)
That's because you're trying to use a handle from a failed connection. Since the connection failed, that handle is invalid. That's why there mysqli_connect_error(), which will return the error message from the LAST attempted connection.
$con_vpn = mysqli_connect(....) or die(mysqli_connect_error());
Note that the connect_error function takes no parameters - it doesn't need any.
I have the following code
function openDBConn($params){
$conn_mode = $params['conn_mode'];
$db_conn = $params['db_conn'];
//create connections
if(empty($db_conn->info)) {
$db_conn = new mysqli("localhost", $user, $password, "database");
$db_conn->set_charset('UTF8');
$mysqli_error = $db_conn->connect_error;
}
if($mysqli_error !== NULL){
die('Could not connect <br/>'. $mysqli_error);
}else{
return $db_conn;
}
}
//close db connection
function closeDBConn( $params ){
$db_conn = $params['db_conn'];
$db_conn->close;
}
//used as below
$db_conn = openDBConn();
save_new_post( $post_txt, $db_conn );
closeDBConn( array('db_conn'=>$db_conn));
From time to time, I get the "Could not connect. Too many connections" error.
This tends to happen when I have Google bot scanning my website.
This problem seems to have started ever since upgrading to MySQLi from MySQL.
Is there any advice on how ensure all connections are closed?
Thanks
You need to increase the number of connections to your MySQL server (the default is only 100 and typically each page load consumes one connection)
Edit /etc/my.cnf
max_connections = 250
Then restart MySQL
service mysqld restart
http://major.io/2007/01/24/increase-mysql-connection-limit/
Some hosters have a hard limit how many open database connections your are allowed to have. Maybe you want to contact your hoster to know how many you are allowed to open. For websites with hight traffic load more connections can be helpful.
Do you have access to the server directly or is it a hosted solution?
If you have direct access you can check the mySQL config files to see how many connections are allowed and increase it.
If you don't you might want to contact your webhost about increasing the limit and see if they will comply.
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);
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..
I am new to php syntax and am looking for advice on creating the most acceptable or correct code. I focus on front end design, but I like to make sure my code is proper. I am in a digital media program, my instructor has given us this code for connecting to our MYSQL databases.
<?php
mysql_connect("localhost", "root", "root")or die("Cannot Connect to DB");
mysql_select_db("Example")or die("cannot select the DB Example ");
?>
However when I look at connect scripts online they set the mysql_connect function as a variable lets say $connect and run an if statement stating; if not $connect produce error, and the same for mysql_select_db. They also close the script with mysql_close($connect); like below
<?php
$connect = mysql_connect("localhost", "root", "root");
if (!$connect)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("Example", $connect);
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysql_error());
}
mysql_close($connect);
?>
Is either one better? What problems can I have if I dont close the connect with mysql_close?
Regarding using an if instead of an or, it doesn't matter. The or short-circuits, so if the connection worked, die(...) won't be executed. Using either is a matter of preference. If you want to use the or version while keeping the result of the mysql_connect() call, simply assign the whole expression to a variable:
$connection = mysql_connect(...) or die('Connection failed.');
mysql_close() should be used after you're done with all your database communication.
The other mysql_*() functions will use the connection created by the latest call to mysql_connect() or mysql_pconnect(). If for some reason you want more than one connection, trusting the implicit connection object in this manner will fail. Better is to store the connection object yourself and passing it in wherever you need it.
Also before starting to work with databases, you should be aware that mysql is not secure anymore and is deprecated, you should use mysqli. You can use it also as object oriented language.
more details : http://www.php.net/manual/en/book.mysqli.php
example :
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>