Check multiple Oracle DB conenction using PHP 5.4 and OCI8 - php

I want to create a simple web page which checks the availability of my several Oracle instances in my lab using PHP 5.4 and OCI 8.
In general it works for all instnaces that are up, but if I have an instance that is down the script exit and the other instances are not checked!??
Let me show you my little code:
<?php
$c1_ORCL11 = oci_connect("system", "mypassword", 'ORCL11');
$c2_ORCL21 = oci_connect("system", "mypassword", 'ORCL21');
$c3_ORCL23 = oci_connect("system", "mypassword", 'ORCL23');
function check_connect ($conn, $sid)
{
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
break;
}
else {
print "$sid: Connected to Oracle!<BR>";
}
// Close the Oracle connection
oci_close($conn);
}
check_connect($c1_ORCL11,'ORCL11');
check_connect($c3_ORCL23,'ORCL23');
check_connect($c2_ORCL21,'ORCL22');
?>
Instances ORCL11 and ORCL21 are UP and ORCL23 is DOWN. If I execute the code I got the following output:
ORCL11: Connected to Oracle!
ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
The idea was to get also "ORCL21: Connected to Oracle!", but the code exit if I can not connect to an instance. If I change the order to:
check_connect($c1_ORCL11,'ORCL11');
check_connect($c2_ORCL21,'ORCL21');
check_connect($c3_ORCL23,'ORCL23');
I get the following output:
ORCL11: Connected to Oracle!
ORCL21: Connected to Oracle!
ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
and if I change the order to:
check_connect($c3_ORCL23,'ORCL23');
check_connect($c1_ORCL11,'ORCL11');
check_connect($c2_ORCL21,'ORCL21');
ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
So, If I have a bunch of Instances and the 1st one in the list is DOWN, the other instances are not shown on my web page even if there are UP and running :-(
Any ideas how to fix this?
Thanks in advance for your help!
Regards,
Ralf

After posting the question I saw my mistake ... strange, looked before some hours and didn't find it ... anyway ... it worked fine after removing the "break;" command within the if case. My assumption was to just break out of the if case and continue but it looks like the break exit the whole code at this stage!
Here is the complete code an the result:
<?php
$c1_ORCL11 = oci_connect("system", "<mypasswd>", 'ORCL11');
$c2_ORCL21 = oci_connect("system", "<mypasswd>", 'ORCL21');
$c3_ORCL23 = oci_connect("system", "<mypasswd>", 'ORCL23');
function check_connect ($conn, $sid)
{
if (!$conn) {
$m = oci_error();
echo "$sid: ", $m['message'], "<BR>";
//break;
}
else {
print "$sid: Connected to Oracle!<BR>";
}
// Close the Oracle connection
oci_close($conn);
}
check_connect($c3_ORCL23,'ORCL23');
check_connect($c1_ORCL11,'ORCL11');
check_connect($c2_ORCL21,'ORCL21');
?>
The Output:
ORCL23: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
ORCL11: Connected to Oracle!
ORCL21: Connected to Oracle!
Regards,
Ralf

Related

Library List is not being used in remote PHP DB2 connection to IBM i

I've successfully connected to a remote IBM i DB2 database (AS400) from my local Windows PC via PHP. I'm using the IBM Data Server Client in conjunction with the db2_* functions in PHP. The problem I'm having is that despite my library list being set properly, it is not being used for unqualified table names. Instead it uses the current user name as the library. However, when I qualify the table names everything works like a charm.
I've confirmed that my library list is actually changing when I create the connection by querying QSYS2.LIBRARY_LIST_INFO.
$database = '<database name>';
$user = '<user name>';
$password = '<password';
$port = <port>;
$options['i5_naming'] = DB2_I5_NAMING_ON;
$options['autocommit'] = DB2_AUTOCOMMIT_OFF;
$options['i5_libl'] = 'MYLIB YOURLIB ANYLIB';
$conn = db2_connect($database, $user, $password, $options);
if ($conn) {
echo "Connection succeeded."; //It succeeds
}
else {
echo db2_conn_error()." | ".db2_conn_errormsg()."<br />";
echo "Connection failed.";
}
$sql = "SELECT * FROM QSYS2.LIBRARY_LIST_INFO";
//Works and proves my library list reflects
//what I passed in when creating the connection.
//$sql = "SELECT * FROM LIBRARY_LIST_INFO";
//Generates: "42S02 : [IBM][CLI Driver][AS] SQL0204N "<user name>.LIBRARY_LIST_INFO" is an undefined name. SQLSTATE=42704 SQLCODE=-204"
//where <user name> is the username used to connect to the DB.
//It should be using the library list specified when creating the connection though.
//This holds true for any table from any library including those specified
//when creating the connection (which includes QSYS2).
$stmt = db2_prepare($conn, $sql);
$result = db2_execute($stmt);
if($result){
while($row = db2_fetch_assoc($stmt)){
echo "<pre>";
var_dump($row); //In addition to entries for QSYS, QSYS2, QUSRSYS and QHLPSYS I get entries for MYLIB, YOURLIB and ANYLIB.
echo "</pre>";
}
}else{
echo "failed<br />";
echo db2_stmt_error()." : ".db2_stmt_errormsg()."<br />";
}
Has anyone ever run into this while enabling i5_naming when connecting to a remote DB2 server? I'm not really sure why it wouldn't be using my library list as the PHP manual states "Unqualified files are resolved using the library list for the job." when enabled. http://php.net/manual/en/function.db2-connect.php
I finally solved this after opening a PMR with IBM. All I had to do was apply the latest Fix Pack for DB2 Connect Personal Edition.
Suggested Fix Packs for DB2 Connect:
http://www-01.ibm.com/support/docview.wss?rs=71&uid=swg21321001
Basically the DB2 Connect version I had was released prior to 2013. It was in 2013 IBM added two tier support by adding the i5_naming option. So my DB2 Connect setup was effectively ignoring the option I was passing. So that explains why the other options still went through. On the DB side, since it didn't receive a value for i5_naming - it remained as the default.

Mongo db connectivity Error?

<?php
$username='root';
$password='xyz';
$database='abc';
$host='localhost';
function MongoConnect($username, $password, $database, $host) {
$con = new Mongo("mongodb://{$username}:{$password}#{$host}"); // Connect to Mongo Server
$db = $con->selectDB($database); // Connect to Database
}
$collection=$db->movie;
$document = array( "name" =>"Calvin and Hobbes");
$collection->insert($document);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $document) {
echo $document["name"] . "\n";
}
?>
I had installed MONGO DB and tried to test my DB, but I am getting an ERROR
"Internal Server Error 500"
And also my Test.php file have my own content called Hello World, but if I had run the TEST.php file it displays Nothing.
My DB table is not accessing and I wasn't able to retrieve data from my Database.
So Kindly help me out here.
There can be several things wrong.
First - is Mongo driver installed?
Second - your MongoConnect function have no effect. You are defining it and not calling. Plus even if you would call it there would be no effect as $db is only in function scope and not outside.
Third - because function MongoConnect have no effect "$collection=$db->movie;" will result in problem as $db is not defined.
Consult http://php.net/manual/en/mongocollection.insert.php on how to insert data in collection.
Internal Server Error only occured when misspelled in code or some of function called wrongly. Please review ur code.

PHP/MySQL query not working with no error

I have written that seems to not be working, but MySQL does not return any error. It is supposed to get data from database1.table to update database2.table.column
<?php
$dbh1 = mysql_connect('localhost', 'tendesig_zink', 'password') or die("Unable to connect to MySQL");
$dbh2 = mysql_connect('localhost', 'tendesig_zink', 'password', true) or die("Unable to connect to MySQL");
mysql_select_db('tendesig_zink_dev', $dbh1);
mysql_select_db('tendesig_zink_production', $dbh2);
$query = " UPDATE
tendesig_zink_dev.euid0_hikashop_product,
tendeig_zink_production.euid0_hikashop_product
SET
tendesig_zink_dev.euid0_hikashop_product.product_quantity = tendesig_zink_production.euid0_hikashop_product.product_quantity
WHERE
tendesig_zink_dev.euid0_hikashop_product.product_id = tendesig_zink_production.euid0_hikashop_product.product_id";
if (mysql_query($query, $dbh1 ))
{
echo "Record inserted";
}
else
{
echo "Error inserting record: " . mysql_error();
}
?>
The manual page for mysql_error() mentions this about the optional parameter you're omitting:
link_identifier
The MySQL connection. If the link identifier is not
specified, the last link opened by mysql_connect() is assumed. If no
such link is found, it will try to create one as if mysql_connect()
was called with no arguments. If no connection is found or
established, an E_WARNING level error is generated.
So it's reading errors from $dbh2, which is the last connection you've opened. However, you never run any query on $dbh2:
mysql_query($query, $dbh1 )
Thus you get no errors because you are reading errors from the wrong connection.
The solution is to be explicit:
mysql_error($dbh1)
As about what you're trying to accomplish, while you can open as many connections as you want, those connections won't merge as you seem to expect: they're independent sessions to all effects.
All your tables are on the same server and you connect with the same users, there's absolutely no need to even use two connections anyway.
You can't just issue a cross-database update statement from PHP like that!
You will need to execute a query to read data from the source db (execute that on the source database connection: $dbh2 in your example) and then separately write and execute a query to insert/update the target database (execute the insert/update query on the target database connection: $dbh1 in your example).
Essentially, you'll end up with a loop that reads data from the source, and executes the update query on each iteration, for each value you're reading from the source.
I appreciate everyone's help/banter, here is what finally worked for me.
<?php
$dba = mysqli_connect('localhost', 'tendesig_zink', 'pswd', 'tendesig_zink_production') or die("Unable to connect to MySQL");
$query = " UPDATE
tendesig_zink_dev.euid0_hikashop_product, tendesig_zink_production.euid0_hikashop_product
SET
tendesig_zink_dev.euid0_hikashop_product.product_quantity = tendesig_zink_production.euid0_hikashop_product.product_quantity
WHERE
tendesig_zink_dev.euid0_hikashop_product.product_id = tendesig_zink_production.euid0_hikashop_product.product_id";
if (mysqli_query($dba, $query))
{
echo "Records inserted";
}
else
{
echo "Error inserting records: " . mysqli_error($dba);
}
?>

PHP - How do I tell why mssql_connect() didn't work?

I'm using PHP 5.2, and normally mssql_connect works fine - but I'm trying to connect to a new MS SQL server, and it won't connect. I've probably got something wrong in the connection details or credentials, but I have no way of telling as I can't get an error message.
The mssql_connect() method returns false, and no connection is available.
mssql_get_last_message() returns nothing - so how do I tell why my connection failed?
Anyone got any ideas?
In MySQL I'd use mysql_error - but there doesn't seem to be an equivalent for ms_sql.
[EDIT]
This question is not a duplicate of "MSSQL_CONNECT returns nothing - no error but no response either" - I'm using php 5.2, and the code works fine for other connection details. I need to figure out how to output what the connection error is - not what the problem is with connecting.
[EDIT2]
To clarify:
the extension is enabled on the server
the code works fine for existing connection details
I am using new connection details and don't know if the hostname or password is wrong
I know the code isn't connecting because I check to see if it's connected
I want to find out the connection error - like you can with MySQL.
Sample code below:
$db_connection = mssql_connect($host, $user, $pass);
$db = mssql_select_db($dbname, $db_connection);
if (!$db_connection) {
echo "No connection";
echo "connection failed because: " . ???????;
die;
}
What do I put in place of ??????? to get the connection error message?
//Database Connection by PDO (MS SQL Server, PHP)
$serverName='localhost';
$database='TreeDB';
$uid='sa';
$pwd='sa*5234';
$dbo = new PDO("sqlsrv:server=$serverName ; Database = $database;ConnectionPooling=0", $uid, $pwd);
//var_dump($dbo);
//Database Query
$sql = "SELECT node.id, node.name, COUNT(parent.id) - 1 AS depth, node.lft, node.rgt
FROM dbo.OrgTree AS node CROSS JOIN dbo.OrgTree AS parent
WHERE (node.lft BETWEEN parent.lft AND parent.rgt)
GROUP BY node.id, node.name, node.lft, node.rgt
ORDER BY node.lft";
//Execute Query
$sql = $dbo->prepare($sql);
$sql->execute();
$qry = $sql->fetchAll();
foreach ($qry as $fetch)
{
$tree[] = $fetch;
}
//print_r($tree);
See below
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = 'KALLESPC\SQLEXPRESS';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
In this way you can display error if connection fails.
Ref: http://www.php.net/manual/en/function.mssql-connect.php

no database selected when opening a php file

I'm trying to create a table with php so I could display data from a database in a browser. I'm using apache, php, mysql and phpmyadmin. the php code is fine - all the examples I've seen on various threads show the same thing.
the problem is when I try to open a php file in a browser I get either ' Invalid argument supplied for foreach()' or 'no database selected'. this probably a basic mistake that I'm doing - but I'm a complete beginner. thanks in advance for any help.
php code below:
<?php
include_once('config.inc.php');
try{
$db = new PDO("mysql:dbname=$db_name;host=$db_server", $db_user, $db_pw);
} catch(PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
print "<div><h3>Title</h3>";
print "<table><tr><th>Column_1</a></th><th>Column_2</a></th></tr>";
foreach ($db->query("SELECT sth_one, sth_two FROM the_database") as $a) {
$row="<tr><td>$a[sth_one]</td><td>$a[sth_two]</td></tr>\n";
print $row;
}
print "\n</table></div>\n";
?>
Could you supply an example for what you're doing?
Generally this indicates, that you forgot to specify which database to use, while connecting to the mysql server
I'm not sure but I think I read something about the right sequence of parameters to insert in the constructor.
Try with:
$db = new PDO("mysql:host=$db_server;dbname=$db_name", $db_user, $db_pw);
or:
$db = new PDO("dbname=$db_name;mysql:host=$db_server", $db_user, $db_pw);
as indicated here: http://php.net/manual/en/pdo.connections.php

Categories