How do I test for an Oracle connection - php

I'm trying to connect to an Oracle DB which is currently offline. When it's online it's not a problem, however, now that it's offline my program is getting hung up on the $connection = oci_connect() line and timing out. How do I simply check the connectio and bail out if it's not there?

Try this (fill in your ip and port):
if ( #fsockopen($db_ip,$db_port ) ) {
//connect to database
} else {
// didn't work
}

This gives you both a manual error, plus will return the actual error.
$connection = oci_connect() or die("Critical Error: Could not connect to database.\n\n". oci_error());
Make sure to test this as hopefully the Oracle error doesn't do something stupid like return the connection string (with your DB password) but I wouldn't assume until you see for yourself.

You could select null from dual.
OK, now I see what your asking, I think.
You want to know how to tell if a database is up before you connect to it?
You can use TNSPING to see if a database is up... ok, maybe that's not 100% accurate but it's a good indicator. go to a command prompt and type TNSPING and hit enter. So then you have to figure out how to call a command line tool from PHP.

Here is what I do in ASP.NET
Dim OracleConn As New OracleConnection(YOUR CONNECTION STRING HERE)
Try
OracleConn.Open()
OracleConn.Close()
Catch ex As Exception
Session("ErrorMessage") = "OracleConn: " & ex.Message
Response.Redirect("AccessDenied.aspx")
End Try
It doesnt necessarily say the DB is offline, but an exception will occur if the connection cannot be opened

Related

Connect to SQLite using PHP - Cannot Connect

I'm trying to use PHP to connect to SQLite. I created a database by importing a CSV file into the tables for three tables. However, I'm unable to connect using the following code:
$dbhandle = sqlite_open('db/pokedex.db', 0666, $error);
if(!$dbhandle) die ($error);
This returns the following error:
Warning: sqlite_open() [function.sqlite-open]: file is encrypted or is not a database in /pokedex/configpokedexdb-sqlite.php on line 12
file is encrypted or is not a database
Googling told me I might have a version mismatch. Despite finding some SQLite3 mentions in my phpinfo(), I decided it might still be a problem so I tried the following suggested code:
try
{
//connect to SQLite database
$dbhandle = new PDO("sqlite:db/pokedex.db"); //sqlite:VPN0.sqlite
// echo "Handle has been created ...... <br><br>";
}
catch(PDOException $e)
{
echo $e->getMessage();
echo "<br><br>Database -- NOT -- loaded successfully .. ";
die( "<br><br>Query Closed !!! $error");
}
After which I received the following error:
Warning: sqlite_exec() expects parameter 1 to be resource, object given in /home/rawdco81/public_html/pokedex/index-sqlite.php on line 53
Before this, I tried running new PDO("sqlite:VPN0.sqlite"); which was what the site provided, but that was obviously wrong because it didn't point to my .db file at all. You'll see this piece of code in the comments beside the function call.
I'm having a hard time just connecting to the database...What is the proper way to do this?
Also, I'm running PHP Version 5.2.13.
EDITED: I pasted the wrong error message in the wrong place.
Now that I look more closely.., I think you are connecting successfully in the second code segment! You shouldn't be using sqlite_exec in tandem w/ PDO though; those are two different PHP interfaces into SQLite.
The reason the first bit of code bombed is because the legacy library doesn't support PDO v3. The second bit of code is bombing once you try to run sqlite_exec against the PDO object I presume.
I bet if you put a var_dump($dbhandle); after the try/catch you'll see you've got an initialized PDO object.
Just read up on using PDO to run queries and you should be golden!

mysql_query() expects parameter 2 to be resource error in connection with remote mysql DB

My PHP code works well in connecting remote windows system mysql database and returns the output. But, when I'm using the same to connect remote linux system's mysql database, I got the following error:
"mysql_query() expects parameter 2 to be resource, boolean given in
C:\wamp\www\mysqldb.php on line 88"
That line 88 have the following content "$this->resultQur =
mysql_query($query, $this->connID);"
Help me to solve this.
yes. The resource is null in this case. But the same works in windows mysql connection. I got the error only in linux. Need to do any change for linux environment?
While putting "print mysql_error();" i got the following error
"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."
The resource you're providing comes from a mysql_connect and that did not succeed!
Put error reporting on
Build some basic error handling in your script
Do not use mysql_* but mysqli_* or even better PDO with parameter binding
Output returned string from mysql_error() after your query. In that way you will see actual error.
$this->resultQur = mysql_query($query, $this->connID);
print mysql_error();
mysql_connect returns a resource on success, but a boolean "false" on error.
Your connection attempt probably failed an so the mysql_query won't be successful.
Try something like the following to see what exactly is causing the error.
mysql_connect(..) or die(mysql_error());
Additionally, it seems that the "old" mysql-library get's deprecated in PHP and it's recommended to switch to a more modern version, eg mysqli or PDO.
1 . Firstly put ini_set(‘display_errors’,1);error_reporting(E_ALL|E_STRICT); in your code at the start of the page
2 . Put Try catch around the query and print the Exception message
try { enter code here }catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
3 . Use PDO then the mysql object for query it's a modern and better approach
Use try catch around the the query and try

Do this method can blow off the 2006 mysql error

Few days back i suddenly came up with getting empty result, I was surprised and then check out the mysql_error function and then got this 2006: Mysql server has gone away. The problem is that it doesn't comes everytime, but sometimes only for the same query.
I have checked out many questions here at stackoverflow as well as many blogs on the web, but I found no possible solution for this error
Today, I wrote this function for that and the error gones away,
$conn = mysql_connect($this->host, $this->dbUser, $this->dbPass);
$db_select = mysql_select_db($this->dbName, $conn);
for($i=0;mysql_error($conn) != 2006;$i++)
{
$result = mysql_query($sql, $conn);
if($i > 10){break;}
}
I wanna ask the professionals that if it it is the proper way of getting rid of this kinda situation or it is the wrong method for that
This error will occurr when the MySQL server has been restarted / unavailable. It could be happening on a specific query if the query is causing some kind of problem (can't say more without knowing what the query does).
Either way, this error is valid and shouldn't be ignored. You need to find out WHY it is being raised in the first place.
You can use the mysql_ping function to check if the server is available before running a query rather than catching the error.

mysql Link to server lost, unable to reconnect

I get the following error:
Link to server lost, unable to reconnect
I have a mysql daemon coded, and I use mysql_pconnect to connect to mysql but after a while, I get the following error and the daemon stops functioning properly:
Link to server lost, unable to reconnect
What I do is the following:
while(true)
{
$connect = mysql_pconnect(...);
$db = mysql_select_db(...);
}
What can I do to prevent this? I need mysql connection to stay steady for the whole duration of the daemon - which may be forever.
You have a few choices.
But just as a precursor, you should try and move away from relying on mysql_* and start using the PDO instead.
Anyway...
When you open a mysql connection, it will stay "available" until the wait timeout has expired. What you are doing is constantly creating a new connection, (also without closing the other connection). This means you will either hit the server mysql limit, or your unix box socket limit very quickly.
Wait Timeout
You should first check with your server to see what this timeout is set to.
You might want to consider increasing it in your my.cnf if it is terribly low. The default period is 28800 seconds (if I recall correctly).
You can check by issuing this query:
SHOW variables like "%wait_timeout%"
You can then change the value in your my.cnf to increase it or you can also set it using
SET ##GLOBAL.wait_timeout=288000
SELECT 1
Okay, now, with a reasonable set timeout set, the "typical" way that you can make sure that you don't get the mysql has gone away message, is to just do a SELECT 1. This will do another query and make sure that the connection is held open.
And, the benefit of using the PDO is that you can then catch PDOExceptions which might be thrown when a SELECT 1 fails.
This means, that you can then try and connect again, if an exception is thrown, and then check again. If you can't connect after that then you should probably kill your daemon.
Here is some code.... you would clearly have to make your PDO object using a valid connection string.
// $pdo holds the connection and a PDO object.
try {
$pdo->execute("SELECT 1");
} catch (PDOException $e) {
// Mysql has gone away.
$pdo = null
$pdo = new PDO( $connectionString );
echo "Mysql has gone away - But we attempted to reconnect\n";
try {
$pdo->execute("SELECT 1");
} catch (PDOException $e) {
echo "Mysql has failed twice - Kill Daemon\n";
throw($e);
}
}
This is the solution that I would probably take.
You can easily substitute the SELECT 1 with your own query that you actually want to use. This is just an example.

Why did my PHP stop working?

I have some complex code. Complex, but it was working.
The I wanted to add some new code, realized that something needed to become a function and then went on a refactoring rampage. Now my code no longer works.
So I did a bit of file compare, some code reading and debugging, and convinced myself that my changes hadn't broken anything.
To test this theory, I put together an exceedingly simple test program:
<?php
$connection = odbc_connect("Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=mysql;Option=3;", "root", "");
var_dump($connection);
echo '<br>';
$result = #odbc_exec($connection, 'show version()');
var_dump($result);
?>
which resulted in
resource(2) of type (odbc link)
bool(false)
The strange thing is that the odbc_connect() succeeds, but the simplest MySql command I can think of fails.
Btw, I have tested at the command line & the MySql server is up & running (Xampp) and reports v 5.1.41.
Obviously I am overlooking something very basic, but what?
This is the simplest MYSQL query I can think of:
select 1
This should help you determine if your connection is working or if the problem lies elsewhere.
Maybe the odbc driver "wants" to tell you something about what is causing the error...
$result = #odbc_exec($connection, 'show version()');
if ( !$result ) {
printf("error: %d %s", odbc_error($connection), odbc_errormsg($connection));
}
else {
echo "ok";
}
see http://docs.php.net/odbc_error and http://docs.php.net/odbc_errormsg

Categories