Unable to connect PHP to db2 in bluemix - php

I am very much new to PHP programming and bluemix as well. I was looking for connecting to PHP to bluemix. For that I created SQL DB (DB2 database) in Bluemix and bind it to my app also. Then got the credentials and used it in php using db2_connect(). It didnt work and returned connection failed.
After that I got this:
$vcap_services = json_decode($_ENV["VCAP_SERVICES" ]); $db = $vcap_services->{'mysql-5.5'}[0]->credentials; $mysql_database = $db->name; $mysql_port=$db->port; //$mysql_server_name ='${db->host}:${db->port}'; $mysql_server_name =$db->host . ':' . $db->port; $mysql_username = $db->username; $mysql_password = $db->password;
$con = mysql_connect($mysql_server_name, $mysql_username, $mysql_password);
My question is, should I replace mysql with db2? Or it'll run like this just changing mysql-5.5 to sqldb?
I used db2_connect also and it is not working.

For connecting to the SQLDB service in Bluemix you must use db2_connect rather than mysql_connect, the docs are here. As this is a remote database then you must use a connection string rather than separate databaseName, userName parameters.
Here is an example of how to parse the VCAP_SERVICES and connect to the SQLDB service in PHP:
# Decode JSON and gather DB Info
$services_json = json_decode($json,true);
$sqldb = $services_json["sqldb"];
if (empty($sqldb)) {
echo "No sqldb service instance is bound. Please bind a sqldb service instance";
return;
}
$sqldb_config = $services_json["sqldb"][0]["credentials"];
// create DB connect string
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=".
$sqldb_config["db"].
";HOSTNAME=".
$sqldb_config["host"].
";PORT=".
$sqldb_config["port"].
";PROTOCOL=TCPIP;UID=".
$sqldb_config["username"].
";PWD=".
$sqldb_config["password"].
";";
// connect to database
$conn = db2_connect($conn_string, '', '');

You should not use $mysql_connect, this is MySQL-specific. Use $db2_connect, see the docs for references.
In addition, you are most likely using the wrong credentials (or: an empty object instead). Check the service name from your Bluemix console (expand the service credentials under the service instance in your app screen): most likely they are not "mysql-5.5". I recommend you just print $vcap_services to a console or as debug output in your HTML, and see how it looks like to get the right server/port/user/password.

Related

Connecting to Oracle Database 11g using PHP code

I am trying to develop a web application with php that will connect to an 11g oracle database.
Here is my code:
//database parameter
$db_name = "my_base";
$user = "username";
$password = "password";
$sid = "sid_number";
$port = "1521";
//function of connection
$conn = oci_connect ($user, $password, "//localhost/".$db_name);
Is the connection line correct? Also I'm not introducing the port number and sid. Can someone tell me if the port number and sid are required or not?
Your connection string is correct it's not required port number & service number but if aren't able to connect & you changed the default setting you can always add port number & service in your connection string like this [//]host_name[:port][/service_name][:server_type][/instance_name] read more from here http://php.net/manual/en/function.oci-connect.php#refsect1-function.oci-connect-parameters

PHP PDO OCI TNS adatpter error (c:635)

I'm browsing the web for two days in 3 languages, but unfortunately couldn't find an answer (checked the questions here of course, but the one identical was unanswered).
There's a virtual server with apache 2.4,php 5.6.7,oracle instant client 12_1 (32 bit). After a few restarts and ini configuration I managed to use PDO odbc and plain oci_connect(). BUT when I try PDO OCI I get this error message:
Error!: SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12560:
TNS:protocol adapter error (ext\pdo_oci\oci_driver.c:635)
The TNS name I set was successfully used in SQL developer to connect.
I'm fairly new to this environment so please ask for the information you might need.
Try something like this.
$conn = new Pdo("oci:dbname=(DESCRIPTION = (ADDRESS_LIST = (
ADDRESS = (PROTOCOL = TCP)
(HOST = {$params['host']} )
(PORT = {$params['port']} )
))
(CONNECT_DATA = (SID = {$params['servicename']})
)); charset=AL32UTF8",
$params['user'],
$params['password']
);

Connecting PHP to Cloud MYSQL Google Apps

I am trying to connect my google app to a google cloud SQL instance, I have followed all the documentation and have read every page here I can find but no luck so far.
I am connecting using this -
<?php
$host = ":/cloudsql/<your-project-id>:<your-instance-name>";
$user = "root";
$password = " ";
$datbase = "cloudbooks";
mysql_connect($host,$user,$password);
mysql_select_db($datbase);
?>
The error I am getting is
Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?
Any help would be amazing!
First thing's last.. Do not use mysql_ instead use mysqli_ for all the reasons on this you can check this : Stackoverflow mysql_
Moving on from that, there is no reason you should think of connecting to a MySQL server any different just because of how its hosted. Cloud is a wonderfull Buzz word.. But its nothing new, its still just a host.
So you can use the following in your connection construction.
$host = "hostname // IP";
$user = "username";
$password = "password";
$datbase = "your_database";
There is actually some documentation also on this : google 'cloud' MySQL

I am unable to bind to an LDAP server due to "Invalid Credentials", though the credentials are valid

I'm setting up a php page to connect to an LDAP server but for some reason it will not let me connect. At first I thought that my credentials had not been set up correctly, but after entering them into Softerra LDAP browser I was able to connect there.
<?php
$url = "ldaps://ldap.XXX.XXXX.edu:PORT/o=XXXX.edu";
$ldap_user = "uid=XXXXXXXX,ou=Campus Accounts,o=XXXX.edu";
$ldap_pass = "XXXXXXXX";
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
$conn = ldap_connect($url) or die ("Could not connect to server");
if ($conn)
{
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3) ;
$bind = ldap_bind($conn, $ldap_user, $ldap_pass);
}
?>
But all that I get back is the following message.
Warning: ldap_bind(): Unable to bind to server: Invalid credentials
Is there something extra I need to do to the user data to get it to be accepted?
If you are connecting to Active Directory (which is implied by the o=XXX.edu style notation (though if so, incorrect)) and by the comment suggestions of trying to bind as xxxx.edu\xxxx then the root most nodes in Active Directory are always dc= not o= and therefore a more correct bind DN or base DN would most likely finish as:
dc=xxx,dc=edu

FileMaker PHP API Connection Issue

I have recently created a script which should be passed an IP address of the users FM DB Server location, then the script will connect to that server with the given username, password, IP Address and DB Name.
However, no matter what I pass as the IP, it never throws an error.
Is there some form of error handling within the FileMaker PHP API for connection errors?
Thanks in advance!
All the FileMaker API calls return an result object in case of error. You should try this:
Here is an example:
$fm = new FileMaker();
// Set 'hostspec' property using setProperty()
$fm->setProperty('database', $fmConfig['db']);
$fm->setProperty('hostspec', $fmConfig['host']);
$fm->setProperty('username', $fmConfig['user']);
$fm->setProperty('password', $fmConfig['pass']);
$dt = date('m/d/Y H:i:s', $myDate);
$freq = $fm->newFindCommand("myTestLayout_1.0") ;
$freq->addFindCriterion("ModificationTimeStamp", ">".$dt);
$result = $freq->execute();
if (FileMaker::isError($result)) {
$ErrMsg = 'Error code: '.$result->getCode().' Message: '.$result->getMessage();
throw new Exception ($ErrMsg);
}
$foundRecords = $result->getRecords();
echo count($foundRecords)." records";
The server that you're making the calls from needs to have curl support - make sure that's enabled. Best bet is to try locally against your FMS box with the test database - once you've got that working then you can try the remote connection.

Categories