mysql_connect() causing web page to crash - php

I am currently running an amazon EC2 instance to host my website. I am trying to connect to the MySQL database.
I have code ...
<h1>Test page</h1>
<?php
$connection = mysql_connect("xxx.xxxx.us-west-2.rds.amazonaws.com","username","password");
?>
An attempt to load this web page will cause an infinite loading symbol. It is as if an infinite loop is running. Here are some details ...
The page will load if i remove the mysql_connect() function
The page will properly display all php data including all other functions.
The "host","username" and "password" arguments are correct and the MySQL database server is running. I know this because i can connect and manipulate it through Sequel Pro.
The page will load if i modify the "host" argument to something incorrect.
Feel free to ask questions. Thanks.
EDIT: After waiting about 90 seconds, the page will eventually load and display error 2002.

Try using:
mysqli_connect()
instead of
mysql_connect()
So maybe this server only supports mysqli.
I'm not sure what else to try if everything else is right.

This means that the server is not responding to the request you sent and so mysql connection is not being established. Try using another server.

Related

PHPUnit database connection error

Very simple-to-explain problem here (at least after struggling with it and simplifying it almost up to the absurd).
I do a connection via the OCI driver inside a file called whatever.Test.php. Just that, make a simple query, and exit. I then call the file from the browser, and the data taken from the database is properly displayed. Next step: I run PHPUnit over the same file, with the following result:
PHP Warning: oci_connect(): ORA-12154: TNS:could not resolve the connect identifier specified in ...
Any ideas?
The final solution was a little bit obvious once I came up with it. Somehow the server could figure out what the server I was trying to connect with was, via the tnsnames.ora file somewhere, I guess. However, since the script execution doesn't trigger all the server processes this information was not reachable any more from there. So what I needed to do is to provide all the data in the oci_connect() $connection_string parameter, using the format [//]host_name[:port][/service_name][:server_type][/instance_name] (check documentation). Previously I was only giving the host_name part.
(Thanks anyway for your reply, user*).

PHP and SQL - Database overload

I'm building a web app that uses lots of requests to my database. Every thing was working perfectly smooth until a half an hour ago when the requests weren't returning... I checked the PHP file directly and it displays the following:
<br />
<b>Warning</b>: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Too many connections in <b>/home/sanity/public_html/dev/forest/js/database.php</b> on line <b>7</b><br />
Unable to connect to MySQL
So I figured let's check phpMyAdmin, but it's not showing me ANYTHING except for a big red box that says:
SQL query: Edit Edit
SET CHARACTER SET 'utf8';
MySQL said: Documentation
#1045 - Access denied for user 'root'#'localhost' (using password: NO)
Between the last time it worked and now I haven't changed any configurations or code.. How do I begin to fix this?
Could this be caused by the fact my PHP files don't close the connection after using it? If so should I be closing the connection after every query? I figured the connection would close automatically when the user leaves the web site.
EDIT: The requests are sending through now and phpMyAdmin is back up, but how do I prepare this site for heavier traffic?
When I started my job, one of my first tasks was to continue working on what one of the directors had started coding. In his code, I saw this monstrosity:
function getTicket($id) {
mysql_connect("localhost","username","password");
mysql_select_db("database");
$sql = mysql_query("select * from tickets where id = ".intval($id));
return mysql_fetch_assoc($sql);
}
In other words, he was creating a whole new database connection every single time he wanted something from the database, and never closing any of them (instead letting them be closed at the end of the script automatically)
This was fine for basic testing, but as soon as I started writing more advanced stuff (before I'd discovered this piece of code) things majorly screwed up with the same "too many connections" error as you.
The solution was simple: restart the server to clear all pending connections, and fix the code to only connnect once per script execution.
This is what you should do. There should only ever be one call to mysql_connect (or indeed any database library's connect function) in your script (and I don't mean in a function that gets called several times!)
You should also check the database's configuration, just in case someone accidentally set the maximum connections too low, but generally this shouldn't be a problem if you manage your connections properly.
Though the mysql_* functions are deprecated (use a modern driver like PDO for instance), you should take a look at the mysql_close($con) function.
Here is the doc.
EDIT
If you are not using mysql_pconnect function, then your connection should be closed at the end of the execution of your script.
Apparently, one reason for such error is shared hostings. If you are using a shared hosting, generally speaking, the maximum connections to the server allowed by the hosting is not the greatest.
If you can change the max_connections system variable then try to change it to a greater number:
max_connections = 200

error to connect database, target machine actively refused it

I'm developing an application, in which two tables of a database are connected but not the third. (This is on server. They are working fine on my local machine.)
1. Warning: mysql_connect() [function.mysql-connect]: [2002] No
connection could be made because the target machine actively (trying
to connect via tcp://localhost:3306) in D:\Hosting.. on line 5
2. Warning: mysql_connect() [function.mysql-connect]: No connection could be made because the target machine actively refused it. in D:\Hosting.. on line 5
At an earlier stage, I connected to the two tables from the same database successfully, so I've checked the physical location of each table of the database. No wonder all are physically resting at the same place in the database.
Please help me to understand the problem.
EDIT :
//Page1 has something like:
$conid=mysql_connect("dxxs.db.7xx7.hostedresource.com","dxxs","Kxx3") or die();
mysql_select_db("dxxs",$conid) or die();
mysql_query(“some basic 4-5 queries depending on user’s interaction with table1”);
mysql_close($conid);
//redirection to page2 from there to page3
$conid=mysql_connect("dxxs.db.7xx7.hostedresource.com","dxxs","Kxx3") or die();
mysql_select_db("dxxs",$conid) or die();
mysql_query(“some basic 4-5 queries depending on user’s interaction with table2”);
mysql_close($conid);
//some redirection to page4
$conid=mysql_connect("dxxs.db.7xx7.hostedresource.com","dxxs","Kxx3") or die();
mysql_select_db("dxxs",$conid) or die();
mysql_query(“some basic 4-5 queries depending on user’s interaction with table3”);
mysql_close($conid);
Now my problem is when connecting to table 3, the server is throwing an error as
stated above
Where in line 5, MySQL_connect is written. Please help me to understand why table 3 is not getting connected whereas table 1 & table 2 are able to connect. All are resting at the same database on the server.
It sounds like you are calling mysql_connect() once for each table you are working with. You do not need to do this - you only need to connect once for each server you are working with.
The server is probably limiting you to 2 concurrent connections, which is why it is refusing the third.
Try calling mysql_connect() once at the top of your script.
I have found the issue and solved it. Try to use my code using port after hostname:PORT:
$dbh=#mysql_connect('dbxxxxxx.db.1and1.com:3306','db-username','db-password');
if (!$dbh) {
$err_msg='Cannot connect to the database because: '.mysql_error();
}

MySQL query runs okay on test machine, but fails on ISP server

I am testing with MySQL on my home machine using an Apache server under XAMPP with the MySQL settings: Server: localhost via TCP/IP, version 5.5.16.
I have a query: SELECT * FROM project WHERE refno = $refno;
This works perfectly on my test machine. When I try the same thing on my ISP server, which has the following settings: Server: localhost via UNIX socket, version 5.0.92
I get the message:
"supplied argument is not a valid MySQL result resource"
Is the problem the difference in the settings? Is there anything I can do to get it working?
I would recommend you first make absolutely sure you have a valid database connection. Look for mysql_connect in your script, and make sure it is being passed the proper parameters. Look for the code examples on the man page to see how to add die() calls to catch any problems. Using die() is not a practice I would recommend on production code, though. You would usually want to catch and log errors, instead of having the script die.
Once you have made sure your connection returns a ressource, if it still chokes on you make sure you are sending the proper parameter (database name) to mysql_select_db.
Once these forementioned two functions play nice, your query should execute no problem.
Hope that helps, good-luck.

PHP SQL Server Connection

I am using SQL Server as my backend for program.when i am try to connect with database which not shows any type of errors,but it is not working.Which is not getting connection.
My code is like
mssql_connect('servername', 'db_user', 'db_password') or die('Error');
The funny thing is which is not connecting and also which is not show message Error.
How can we enable SQL Server configuration using php program (what is the script for that.not manually). How could we know the actual status of SQL Server in server?
My application is working properly in local;the problem is about public hosting.
turn on error_reporting in your php.ini. it might be some other section of code that is causing the issue, since you say that it doesn't go to die part. try checking first if it even goes to the mysql_connect() part. if possible post a part of your code around the mysql_connect.
I would bet it is localhost, I have, nor do I ever wish to, see the database sitting on the same server as the website. Check in your hosting control panel for the proper connection string.

Categories