PHP Pervasive SQL connection - php

I am connecting PHP to Pervasive SQL and the connection keep resetting
This connection is on Windows Server 2012 using PHP7 on Apache 2.4. I have already created a DNS connection and the test can connect to the database successfully.
<?php
$conn=odbc_connect("brps","","");
if(!$conn) die("Could not connect");
?>

The following code works for me in an x64 environment using PHP 7.31 with the ODBC extension enabled in the PHP.INI. I'm also using the v11.30 x64 Client connecting to a remote PSQL v11 server.
<?php
$conn=odbc_connect("brpp","","");
if(!$conn) die("Could not connect");
$result = odbc_tables($conn);
echo '<div id="top">..</div><table border="1" cellpadding="5"><tr>';
$tblRow = 1;
while (odbc_fetch_row($result)){
if(odbc_result($result,"TABLE_TYPE")=="TABLE"){
$tableName = odbc_result($result,"TABLE_NAME");
echo '<tr><td>' . $tblRow . '</td><td>' . $tableName . '</td></tr>';
$tblRow++;
}
}
echo '</table><hr>';
$result = odbc_tables($conn);
while (odbc_fetch_row($result)){
if(odbc_result($result,"TABLE_TYPE")=="TABLE"){
$tableName = odbc_result($result,"TABLE_NAME");
echo '<div id="' . $tableName . '"> *** ' . $tableName . ' *** top</div>';
$cols = odbc_exec($conn, "SELECT * FROM $tableName WHERE 1=2");
$ncols = odbc_num_fields($cols);
for ($n=1; $n<=$ncols; $n++) {
$field_name = odbc_field_name($cols, $n);
echo $field_name . "<br>";
}
echo '<hr>';
}
}
?>
If the DSN doesn't exist, I get an error:
Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\Wnmp\html\phpodbc.php on line 2
Could not connect
I was getting a System error 998 after installing the PSQL x64 client. A reboot of the machine fixed that error.

Related

Connecting to local Database from remote web server

I am trying to connect to my local Database from the webserver but i get
Fatal error: Call to undefined function odbc_connect()
in -/-/-/7001238/web/s/sage2.php on line 15"
Any help on how to fix issue.
Here is the code i used to connect.
$odbc['dsn'] = "Sage50";
$odbc['user'] = "Peach";
$odbc['pass'] = "XXXX";
$mysql['host'] = "localhost";
$mysql['user'] = "root";
$mysql['pass'] = "";
$mysql['dbname'] = "sagetest";
$mysql['idfield'] = "id";
$debug=true;
// Step 1: Connect to the source ODBC and target mysql database
if ($debug) echo "Connect to " . $odbc['dsn'] . ' as ' . $odbc['user'] . "\n";
$conn = odbc_connect($odbc['dsn'], $odbc['user'], $odbc['pass']);
if (!$conn) {
die("Error connecting to the ODBC database: " . odbc_errormsg());
}
$myconn = mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']);
if (!$myconn)
die("Error connecting to the MySQL database: " . $mysql_error());
if (!mysql_select_db($mysql['dbname'], $myconn)) die("Error selecting the database: " . mysql_error());
// Step 1.5: loop through each table with steps 2-7
$allTables = odbc_tables($conn);
$tablesArray = array();
while (odbc_fetch_row($allTables)) {
if (odbc_result($allTables, "TABLE_TYPE") == "TABLE") {
$tablesArray[] = odbc_result($allTables, "TABLE_NAME");
}
}
Thank you for your time!
First: This error happens because you don't have the ODBC PHP extension installed.
Check http://php.net/manual/en/odbc.installation.php too.
In debian distros you can solve this with a apt-get install php5-odbc, but you can check this also with your hosting provider.
When you see a Call to undefined function you always must check the php.net to be sure about the name of function, or the extension is not loaded.
PS 1: I think you're trying to compare/transfer data between two databases, right?
PS 2: Make sure your server can reach the ODBC address. The webserver is not your dev machine, so localhost is not the real localhost ;)

PHP cannot connect to MS SQL Server 2008 IIS

I did an express install for MS SQL Server 2008 and created a database with a test table. I am running Windows Server 2008 and have IIS, PHP and MS SQL installed. I chose to go with SQL server authentication, where you would need credentials to modify or view the database. I try to print out a simple test table on a web page, but it keeps failing and only returns a white screen..
<?php
$server = "WS1\SQLEXPRESS";
$username = "sa";
$password = "password";
$db = "testdb1";
$dbhandle = mssql_connect($server, $username, $password)
or die ("Cannot connect to SQL Server on $server");
$selected = mssql_select_db($db, $dbhandle)
or die ("Could not open database $db")
echo "You are connected to the " . $db . "database on the " . $server . ".";
$query = "SELECT * FROM table1";
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
echo "<li>" . $row[""] . $row[""] . "</li>";
while ($row = mssql_fetch_array($result)) {
print_r($row);
}
mssql_close($dbhandle);
?>
Ah, the horrific white screen. Sometimes very hard to troubleshoot. Often times it's a simple missing semicolon ...
like the one here:
$selected = mssql_select_db($db, $dbhandle)
or die ("Could not open database $db")
Put a semicolon on the end of that statement and you are back in business.

Connection to MySQL using ODBC driver in PHP code

I have a simple code for selecting data using MySQl and PHP.
Here it is:
<?php
require_once 'login.php' ;
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die ("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to choose DB: " . mysql_error());
$query = "SELECT * FROM bank";
$result = mysql_query($query);
if (!$result) die ("Failed with connection to DB: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j=0; $j < $rows; ++$j) {
$row = mysql_fetch_row($result);
echo 'MFO: ' . $row[0] . '<br/>';
echo 'NAME: ' . $row[1] . '<br/>';
echo 'STATE: ' . $row[2] . '<br/><br/>';
}
?>
But i have to use ODBC driver instead of standard integrated driver.
I've read a lot of information but haven't find anything , so maybe someone help what should i add or rewrite in this code for using ODBC ?
There are a lot of odbc functions in PHP.
http://php.net/manual/en/function.odbc-connect.php
You can connect with this functions to mysql and other databases. In the commments on php.net you can find some examples how to connect to MySQL.
http://www.mysql.de/products/connector/
Here you can find an ODBC driver for MySQL.

Php MSSQL(2012) giving error:Couldn’t connect to SQL Server

I am trying to fetch a set of fields from a Database on MSSQL Database on SQL Server 2012. This is a remote server and I am trying the following piece of code.
//MSSQL Server for retrieving the Member name from Member ID:
//mssql.secure_connection = On
// Need to upload ntwdblib.dll from net
$myServer = "IPAddress/SQLExpress "; // host/instance_name
$myUser = "ID"; // username
$myPass = "pass"; // paasword
$myDB = "dbname"; // database name
// connection to the database
$dbhandle = mssql_connect($myServer, $myUser,$myPass)
or die("Couldn’t connect to SQL Server on $myServer"). mssql_get_last_message();
// select a database to work with
$selected = mssql_select_db("sportsclub", $dbhandle)
or die("Couldn’t open database $myDB");
echo "You are connected to the " . $myDB . " database on the " . $myServer . ".";
$query = "SELECT first_name, last_name";
$query .= "FROM members ";
$query .= "WHERE member_id='".$row['member_id']."'";
// your database query
$result = mssql_query($query);
while($row = mssql_fetch_assoc($result))
{
echo "<td>" . $rows["first_name"] . $rows["last_name"] . "</td>";
}
// close the connection
mssql_close($dbhandle);
//Ended MSSQL Connection
It simply does not connect to the sql server. It gives the error: Couldn’t connect to SQL Server on IPAddress/SQLExpress
I tried checking all configurations like TCP/IP through SQL Server Config management.
Can someone please help?
Ensure remote connections over named pipes are enabled and make sure you're using a backslash before the instance name:
$myServer = "IPAddress\SQLExpress ";
If you'd like to enable connection over the default port (1433) this answer will help.

Oracle Connection Error in PHP. Fatal error: Call to undefined function oci_connect()

I am working on this project where i need to connect to the Oracle database. I am using the latest version of WAMP 2.2 and also activated all the extensions related the Oracle in PHP extensions. I have used following code for the connection to Oracle Database.
<?php
$dbHost = "192.168.0.205";
$dbHostPort="1523";
$dbServiceName = "orcl";
$usr = "system";
$pswd = "admin";
$dbConnStr = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST=".$dbHost.")(PORT=".$dbHostPort."))
(CONNECT_DATA=(SERVICE_NAME=".$dbServiceName.")))";
if(!$dbConn = oci_connect($usr,$pswd,'192.168.0.205:1158/em')){
$err = oci_error();
trigger_error('Could not establish a connection: ' . $err['message'], E_USER_ERROR);
}
else
{
echo "COnnected";
}
$strSQL = "SELECT SYSDATE FROM DUAL";
$stmt = oci_parse($dbConn,$strSQL);
if ( ! oci_execute($stmt) ){
$err = oci_error($stmt);
trigger_error('Query failed: ' . $err['message'], E_USER_ERROR);
};
while(oci_fetch($stmt)){
$rslt = oci_result($stmt, 1); print "<h3>query returned: ".$rslt."</h3>";
}
?>
Kindly suggest the error. Thanks in advance.
The OCI8 PHP extension is not available in your PHP installation. See the manual of the OCI8 PHP extension for installtion instructions.

Categories