I am new with using OBDC in php. I have an error upon connection:
[function.odbc-connect] SQL error [Microsoft][ODBC Microsoft Access Driver] Invalid string or buffer length
PHP ODBC
$string_serve = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = host.com)(PORT = 1404)))
(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = SERVE)))";
$string_serve ="DRIVER={SQL Server};
SERVER='server.com';
DATABASE= db;"
$conn = odbc_connect($string_serve, 'user', 'pass');
I have tried using both variables
I recommend to keep the connection details out of the source code.
Create a .odbc.ini file somewhere not publicly accessible. Define a data source like this:
[my_database]
Driver = /usr/lib/libmyodbc3.so
SERVER = server.com
DATABASE = db
Let the odbc driver find it:
putenv('ODBCINI=/your-application-path/etc/.odbc.ini');
Using exported variables at Apache-startup etc. might not work as avariables are sometimes not passed to the application.
Use a simple odbc_connect() in your PHP code:
$conn = odbc_connect('my_database', 'username', 'password')
Related
I am trying to log into a local SQL database, but I keep getting an error about 'Login Failed'. This is the first time that I have tried to connect to a MS SQL server... normally I use MySQL. So, I am not even sure if I am doing this correctly.
Here is my connection code:
function dbConnection()
{
$host = '127.0.0.1';
$db = 'my_db';
$user = 'dbuser';
$pass = "my_pw";
$serverName = "tcp:$host,1443";
$connectionOptions = array(
"Database"=>$db,
"Uid"=>$user,
"PWD"=>$pass
);
$connection = sqlsrv_connect($serverName, $connectionOptions);
if (!$connection) print_r(sqlsrv_errors());
return $connection;
}
print_r(dbConnection());
Everytime I try this, I get:
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for
user 'dbuser'.
The username and password are correct, and the permission are set correctly. The port is correct, too. I can log into SSMS just fine, but not through PHP.
Any ideas?
Assuming that your mssql server instance is say sqlexpress..
Please use
$serverName = "serverName\\sqlexpress";
or if the port is 1443, then
$serverName = "serverName\\sqlexpress, 1443"
I am trying to make a successful Oracle connection using PHP.
Here is how my connection string looks:
<?php
$conn = oci_connect("USER", "PASS", "LOSINGMINDHOST");
if (!$conn) {
$e = oci_error();
error_log(trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR));
}
oci_close($conn);
?>
Getting the following error:
Warning: oci_connect(): ORA-12170: TNS:Connect timeout occurred
Which points to the line with oci_connect.
I am on a Windows Server 2019.
The php.ini file has been updated to include the following:
extension=oci8_12c
I have confirmed that the dll file above is indeed in the ext folder listed as:
php_oci8_12c.dll
Not sure why the php.ini file does not include the full name of the dll file.
The server has been installed with Ocale 12g instant client 64bit.
We have confirmed a connection using ODBC Data Source Administrator client using tnsnames.ora file.
We also have a listener.ora file the looks like this:
PROD_MIR =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (COMMUNITY = ttp.world)(PROTOCOL = TTP)(Host = LOSINGMINDHOST)(Port = 1524))
(ADDRESS = (COMMUNITY = ttp.world)(PROTOCOL = TTP)(Host = LOSINGMINDHOST)(Port = 1551))
(ADDRESS = (COMMUNITY = ttp.world)(PROTOCOL = TTP)(Host = LOSINGMINDHOST)(Port = 1538))
)
(CONNECT_DATA =
(SERVICE_NAME = PROD)
)
)
We added the TNS_ADMIN path to the environment variables on the server.
We have already restarted the services and even rebooted the server.
As the docs (https://www.php.net/manual/en/function.oci-connect.php) says:
oci_connect ( string $username , string $password [, string $connection_string [, string $character_set [, int $session_mode ]]] ) : resource
connection_string
Contains the Oracle instance to connect to. It can be an ยป Easy Connect string, or a Connect Name from the tnsnames.ora file, or the name of a local Oracle instance.
It was your case, use the proper tnsnames.ora Connection name.
I need to connect to an Azure SQL DB from an external PHP app on shared cPanel hosting.
My host has enabled mssql_connect but wont enable MS's recommended sqlsrv_connect in the shared environment.
I have whitelisted the webserver IP in Azure's Portal, and whitelisted the DB address in cPanel.
The test script seems to try to connect, but cant. No error information is provided: http://app.hivve.com.au/api/
PHP is:
$db_server = "cjweb.database.windows.net:1433"; // update me
$db_username = "username";
$db_password = "password";
$conn = mssql_connect($db_server, $db_username, $db_password);
print_r(mssql_get_last_message());
Has anyone else been down this road? My host wont provide anymore assistance so Im stuck.
Could you please try to connect as shown below? Please look at the user name.
Add [aftermath] on the ~/.freetds.conf:
[aftermath]
database = mydatabase
host = cjweb.database.windows.net
port = 1433
tds version = 8.0
$myServer = "aftermath"
$myUser = cjweb#cjweb
$myPass = your_password
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
What I found in Azure SQL document Quickstart: Use PHP to query an Azure SQL database that maybe you have lost the "UID" agrument.
<?php
$serverName = "your_server.database.windows.net"; // update me
$connectionOptions = array(
"Database" => "your_database", // update me
"Uid" => "your_username", // update me
"PWD" => "your_password" // update me
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
?>
You can first test Alberto Morillo's code.
Hope this helps.
I have set up an Oracle 18c database and am trying to connect to it from a php file but when I run a simple connection test, I receive a server error where it cant't seem to connect. I ran print_r(getLoaded_extensions()); and from the output array it shows that I am currently not using the oci8 extension as I wanted. My connection test file contains the following
#!/usr/local/bin/php
<?php
putenv("ORACLE_HOME=/usr/lib/oracle/18.3/client64")
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ***.***.*.**)(PORT = 1521)))(CONNECT_DATA=(SID=orcl)))" ;
if($c = OCILogon("username", "password", $db))
{
echo "Successfully connected to Oracle.\n";
OCILogoff($c);
}
else
{
$err = OCIError();
echo "Connection failed." . $err[text];
}
I am unsure whether I set my putenv() wrong to the correct location of the oci.dll file or if I need to install the extension in the first place. Thank you
Since you said that you checked your currently used extensions and OCI8 is not present, I would go ahead and install the module and enable it on your server
Most probably, the 18c default installation creates a Container DB (CDB) called ORCL and PDB1 (pluggable DB)..
Run lsnrctl stat to look for the services created.
then, in DB connection, use service_name instead of SID for connection. The value of service_name can be seen in the output of lsnrctl stat
Example.
$db = "(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = patronus.domain.com)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = pdb1.domain.com)
)
)" ;
I have been playing around for PHP with a while. This is how i connect to sql server and it works:
$serverName = "SNAME";
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"USERNAME", "PWD"=>"PASSWORD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$sql = "SELECT X FROM myTable";
$query = sqlsrv_query($conn,$sql);
if ($query === false){
echo "Could not link to SQL Server";
}
while ($row = sqlsrv_fetch_array($query))
{
$ARRAY[] = "$row[X]";
}
Server SNAME is on the local network, and i can connect to it from my computer using Sql Management Studio.
Now looking online and other places I have realized PDO is the better way to go. So I have been trying to connect to my server using PDO, by following examples online. Here is the code:
$username = "USERNAME";
$password = "PASSWORD";
try{
$conn = new PDO('mysql: host=SNAME;port=1433;dbname=DBNAME',$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = $conn->query('SELECT X FROM myTable');
foreach($data as $row) {
print_r($row);
}
}
catch{
echo 'ERROR: ' . $e->getMessage();
}
When i run this i get this on my page:
ERROR: SQLSTATE[HY000] [2002] No connection could be made because the
target machine actively refused it.
Now i know my other information is right since i can use the top block and sql management studio. Is there anyother library or something special needed. Is there problem with my code?
UPDATE
I ran this in my SQL Management Studio to check my port and it gave me 1433.
SELECT DISTINCT local_tcp_port FROM sys.dm_exec_connections WHERE local_tcp_port IS NOT NULL
In one example, you are using sqlsrv_connect, but in the other you tell PDO to connect to a 'mysql:' database.
You need to use the correct DSN string for a SQL Server connection.
$conn = new PDO('sqlsrv:Server=SNAME,1433;Database=DBNAME',$username,$password);
Manual: http://php.net/manual/en/ref.pdo-sqlsrv.connection.php
P.S. Make sure you have PDO_SQLSRV installed. http://php.net/manual/en/ref.pdo-sqlsrv.php