"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\appraisal\database.php on line 5
No database selected"
Why does it show most of the time? But when I refresh the page by entering F5, the error will be gone and will show the output correctly. This doesn't really affects the system but it is quite bothering when our users see the error.
I have the codes here.
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('db_appraisal');
?>
Hoping for your response. Thanks!
Try adding the $conn parameter to the mysql_select_db function call
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('db_appraisal', $conn);
?>
Link to The Manual
If you are writing new PHP code you should really not be using the mysql_* extension as it has been deprecated i.e. it will be removed from PHP sometime soon.
Use either the mysqli_* extension or the PDO one.
Related
I am attempting to write some connection code with PHP to a Oracle database my school is hosting.
I'm using oci_connect() at the moment to make this connection, but it is failing.
$conn = oci_connect('username', 'password', 'hostname/SID');
I can access the oracle database through sqlDeveloper, as well as phpmyadmin, so I know the login information is correct.
I checked the oracle version with select * from v$version;, it shows as 12c Enterprise.
What is wrong with my php code for connecting? Is there a better way to make an oracle connection through PHP?
This is the test code I'm running, from http://php.net/manual/en/function.oci-error.php
<?php
echo "running";
$conn = oci_connect("username", "paswwrod", "address/SID");
if (!$conn) {
$e = oci_error(); // For oci_connect errors do not pass a handle
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
echo "ending";
?>
The string "running" gets echoed, but "ending" does not, the script just stops working when it attempts oci_connect()
have you also tried including the port number to the oracle db server like so?
$conn = oci_connect("user", "pass", "localhost:1234/xe");
I have scoured google, and stackover flow, and just cant get to the bottom of this issue. I cannot get the following php code to connect to SQL. Its a simple php web document, that i am using to test out some things. SQL is sqlexpress 2016, and its running on IIS with php 7.x installed. PHP code executes fine, so its something with the code or the database is my guess. Things I've tried:
I've ran an echo in php to resolve the name, and it resolves it fine.
I've connected from a separate server to the sql server using tcp, and it connects fine.
I've tried both PDO connection, and mysqli and both come back with same error.
The PDO code ive used is:
<?php
$servername = 'RemoteServerName\SqlInstance';
$username = 'iislogon';
$password = 'password';
try {
$conn = new PDO("mysql:host=$servername;dbname=netdata", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
The mysqli code is:
<?php
$servername = 'RemoteServerName\SqlInstance';
$username = 'iislogin';
$password = 'password';
$dbname = 'netdata';
?>
<?php $conn = new mysqli($servername, $username, $password, $dbname); ?>
<?php
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";?>
Both return the same error of host not found. What other issues could be causing this? Im new to coding php so any help would be appreciated.
mysqli and PDO starting with mysql: are supposed to connect to MySQL, not SQLExpress.
If you want to use SQLExpress you should use something like sqlsrv_connect or adjust your pdo string to a SQLExpress compatible one.
Take a look at this thread too.
Look at this description http://php.net/manual/en/pdo.construct.php and specifically:
In general, a DSN consists of the PDO driver name, followed by a colon, followed by the PDO driver-specific connection syntax. Further information is available from the PDO driver-specific documentation.
Are you sure your dsn is correct and you have the PHP module enabled? See http://php.net/manual/en/ref.pdo-mysql.php
I think you didn't escape your backslash with another backslash. Try this:
<?php
$servername = 'RemoteServerName\\SqlInstance';
?>
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.
$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.
I have the following code in db.php to connect to my DB.
<?php
$DB_HOST = "localhost";
$DB_NAME = "db";
$DB_USER = "user";
$DB_PASSWORD = "pass";
$con = mysql_connect($DB_HOST, $DB_USER, $DB_PASSWORD);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($DB_NAME , $con);
?>
In my other script I include it using:
include("db.php");
In some cases I receive the ff error:
[10-Mar-2012 10:47:20] PHP Warning: mysql_connect() [function.mysql-connect]: User db_user already has more than 'max_user_connections' active connections in /home/user/public_html/sc/db.php on line 8
Now, I am wondering if I need to close the connection like:
<?php
include("db.php");
//other stuff here
mysql_close($con);
?>
BTW, I have a value of 100 max_connections in my MySQL config.
I also research about persistent connection and I believe my code above is not a persistent connection.
No, this won't help you if you close it at the end of the script. mysql_close() is just useful in case you want to free up resources before you end your script, because your connection is closed as soon as execution of the script ends
If you don't close your connections, they will stay open and take up precious resources on the server. I guess there's there the security point too, you don't want to risk someone getting a hold of the connection.
I prefer to put my database in a class and use __construct to create the connection, and __destruct to close the connection. If your unfamiliar with classes. The __construct and __destruct gets called automatically when you create and destroy a class.
Edit:
There was originally meant to be an example. But I have a basic but working mysql class here https://stackoverflow.com/a/9651249/1246494.
It shows the usage of mysql_close and how I was trying to relate it to the class destructor. The point was, any network connection should be closed, whether your database is on a remote server, or localhost.