I'm having issues getting the mssql extension to work in php. Right now I'm using this following function to connect:
function db_open_connection(&$mssql_connection) {
$mssql_username = "XXXXXXXXX";
$mssql_password = "XXXXXXXXX";
$mssql_server = "XXXXXXXX";
$mssql_connection = mssql_connect($mssql_server, $mssql_username, $mssql_password);
if (!$mssql_connection) {
die("Could not connect to the database. The server said ". mssql_get_last_message());
}
}
and this returns
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: XXXXXXXX in XXXXXXXX\mssql.php on line 6
Could not connect to the database. The server said
I am able to connect to the database using the COM object with
$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");
connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
$conn->open($connStr);
though that is a much less coder-friendly interface and I'd rather not have to do it that way if I can help it.
I think the problem is in the string I am attempting to use for $mssql_server. I've read and read through the documentation but I can't figure out what it needs to be, possibly due to my extreme unfamiliarity with non-MySQL databases.
Can anyone point me in the right direction?
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 below obfuscated connection string in SQL developer which works:
jdbc:oracle:thin:#//xyz-scan.example.com:1521/mydb.example.com
How can I use this in php oci_connect?
$db = 'xyz-scan.example.com:1521/mydb.example.com';
$con = oci_connect('scott', 'tiger', $db, 'AL32UTF8');
Lead to error:
ORA-12545: Connect failed because target host or object does not exist
I can ping the server successfully.
I also tried
$db = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST = xyz-scan.example.com)(PORT=1521))
(CONNECT_DATA=(SERVER=DEDICATED)
(SID=mydb.example.com)';
And instead of SID with service_name. Nothing works.
Above gives this error:
ORA-12154: TNS:could not resolve the connect identifier specified
How do I convert this connection string to work with php oci? (is there a unique way? For a different db I have one with #ldap://... how would I convert that?
The solution to first issue with scan address is simple:
$db = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST = xyz-scan.example.com)(PORT=1521))
(CONNECT_DATA=(SERVER=DEDICATED)
(SID=<sid>)';
The second one with ldap is not so simple. As far as I understood oracle has it's own ldap thingy and in it you store the databases TNS entries. This means you query it like you query any ldap system:
<?php
$ds=ldap_connect("oid.mydomain.com", myport); // Connect to oracle ldap
$r=ldap_bind($ds); // Bind to ldap
$sr = ldap_search($ds, "cn=OracleContext,dc=xyz,dc=abc,dc=com", "cn=dbname"); // Run query xyz.abc.com
$info = ldap_get_entries($ds, $sr); // Get entries
ldap_close($ds);
$dbconnectstring = $info[0]["orclnetdescstring"][0]; // Extract db connect string from ldap search result array
$con = oci_connect('scott', 'tiger', $dbconnectstring);
?>
This script will get full TNS connection string which you can then use with oci_connect.
I'm trying to connect to my SQL database and I'm getting the error mentioned above.
This is the PHP code
// This file has database info variables (password etc.)
include "db_config.php";
class DB_CONNECT
{
function __construct()
{
$this->connect();
}
function __destruct(){}
function connect()
{
$connect = mysql_connect($db_server, $db_user, $db_password) or die();
$db = mysql_select_db($db_name) or die();
echo "Connection successful!";
return $connect;
}
}
// Just to try out if the connection is successful
new DB_CONNECT();
This is the error
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /hermes/bosnaweb05a/b1/ipg.matejhacincom1/dbtest/db_connect.php on line 16
The funny thing is that I can connect to the database easily with this code which is generated for db testing by my hosting provider:
$link = mysql_connect('my db server', 'db user', 'db pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db(my db table);
Could someone let me know what I'm doing wrong? I have no idea anymore.
The reason I mentioned that I'm not the server adming is because I saw a lot of questions like this here, but all of them have instructions how to solve it by using some server commands etc.
Either:
There isn't a MySQL server running on the same machine as the webserver or
The MySQL server is configured to place its socket file somewhere other than /var/run/mysqld/mysqld.sock (in which case you need to specify where it is or
The MySQL server is configured to listen only on TCP/IP (which is inefficient but it can be accessed by using the 127.0.0.1 IP address instead of localhost).
Probably in variables $db_server, $db_user, $db_password you have wrong data. You must pass to object data from db_config.php file. For example through constructor.
Have the same problem on an customer server, last week, then :
Be sure your $db_server is according your hosting ; not a remote server.
Then use 127.0.0.1 instead of localhost
In my case, the MySQL server was different the PHP.
Thank you for all of your answers but what solved my problem was nothing that was in any of your answers.
All I did was used PDO instead of deprecated mysql and it worked. I also for some reason needed to move inclulde "db_config.php" in to the class because variables didn't work otherwise.
Anyway, here's the code that now works:
class DB_CONNECT
{
function __construct()
{
$this->connect();
}
function __destruct(){}
function connect()
{
include "db_config.php";
$connect = new PDO($db_server, $db_user, $db_password) or die();
echo "Connection successful!";
return $connect;
}
}
// Just to try out if the connection is successful
new DB_CONNECT();
I just try to connect my php code to postgres database but unfortunately it is not work, here is my code :
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=public";
$credentials = "user=postgres password=postgres";
$db = pg_connect( "$host $port $dbname $credentials" ) or die('Could not connect');;
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
browser result :
Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: database "public" does not exist in /var/www/test/pgtest.php on line 11
anybody know what is wrong here ?
Are you sure the name of database is 'public'? As fas as I remember 'public' is a name of default schema inside the PostgreSQL database.
Please make sure the database 'public' really exists.
From the command line, become the postgres user, start psql, and issue the \list command to list your databases. Be sure the database you're trying to open is in the list.
Be sure your pg_hba.conf file allows the database to be opened.
Remove everything except $dbname from the pg_connect call and allow the API to use the defaults. Add parameters back as indicated by the error message(s).
sorry but finally I found that my question was wrong, public and testdb are Schemes inside postgres database,
I’ve just finished writing a website which is working very well on my local server (xampp).
The following is my connection database definitions for the local server:
<?php
$host="localhost";
$username="root";
$password="1234";
$db_name="partnership";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>
I’ve loaded my files successfully and put them on the remote server (www.mydomain.com) and I’ve created the relevant database ‘partnership’ (successfully imported all Tables from phpMySQL local server to the remote server).
I can view the website (www.mydomain.com) but with no connection to the database (MySQL).
The error message is of course "cannot connect".
I’ve changed $host="localhost" to $host=”www.mydomain.com” but still getting the same error message.
Any assistance on the above issue would be greatly appreciated.
Shouldn't you still use localhost as $host if you're trying to connect via the application? The script is on the same server as the DB.
If you are using cPanel, when you create the MySQL table, make sure you assign your user to it (trivial, I know, but Ive forgotten about it a few times myself.)
If your script is running from your remote server, just stick with localhost. Will work fine.
Last, my only suggestion until a better answer is said, try using
$host = 'localhost';
$user = 'username'; // Im pretty sure your username isn't root.
$pass = '1234';
$db = 'partnership';
/* Alot of hosts like to append your cPanel login to your db and username fields.
Check to see what your table is. It might actually be 'youruser_partnership' */
$mysqli = new mysqli($host, $user , $pass, $db);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
If your host supports mysqli (most do). If not, when you do your die statement, still use die(mysql_error()); to get the exact error. It will more than likely be your username/password you created and assigned to your database
Ah, this is always the most annoying part of the process. Try this and see if it works:
$host="127.0.0.1"; // "localhost" should work, but don't count on it
$username="user"; // username you use to log into phpMyAdmin
$password="password"; // password you use to log into phpMyAdmin
$db_name="dbname"; // database you want to connect to
You'll need to have created this user/password pair and given them the appropriate permissions to access the database. Also note that the mysql PHP functions are deprecated and should not be used. PDO or mysqli are the preferred ones going forward.