access oracle DB on remote linux server using php - php

I want to access an Oracle database that is on a remote Linux server with my company's intranet.
Currently I can access it through Excel and a System DSN on my Windows 7 machine. But I am running into difficulties with multiple queries in my excel spreadsheet, so I want to create a web page that does the same thing using PHP.
I have the name of the server that the DB is on, but I cannot get any response, either good or bad from my queries. I have read about web
Here is an example of the connection script:
//Data Source=username/password#//myserver:1521/my.service.com;
echo "set up db connection.<Br>";
$db = oci_connect('userid', 'password', 'server/db');
if (!$db) {
echo "no db. <br>";
$e = oci_error();
if (!$e) {
echo "no oci errors. <br>";
}
else {
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
}
else {
echo "DB, yes <br>";
}
if ($db->connect_error) {
echo "Connection errors. <br>";
die('Connect Error ('. $db->connect_errno .') '. $db->connect_error);
}
else {
echo "no connection errors.<br>";
}
echo 'Success, db info: '. $db->host_info .'.<br>';

Related

Using php I need to have 2 different connections to the database

I have a need to copy some rows from a database to a different database. I am experiencing difficulty. I have found several methods except none of the seem to work. The php version I am using is 5.4.
Both connections are in the same server, however everything else is different
This is the php code that I have found and it doesnt seem to work at all, I am unable to select from the first database
// Create connection
$wpdb = mysql_connect($servername, $username, $password);
// Check connection
if ($wpdb->connect_error) {
die("Connection failed: " . $wpdb->connect_error);
}
echo "Connected local successfully\n";
//$starttime = date("h:i:sa");
$mydb = mysql_connect('localhost','dbname','dbpassword', true);
// Check connection
if ($mydb->connect_error) {
die("Connection failed: " . $mydb->connect_error);
}
echo "Connected to Integrity successfully\n";
mysql_select_db($database, $wpdb);
mysql_select_db('wordpress_0', $mydb);
you can try with PDO.that provide a common interface to talk with many different databases.
$pdo = new PDO('mysql:host=example.com;dbname=database', 'user',
'password');
I refuse to offer support for mysql_ syntax, so I'll offer the upgraded version.
Almost entirely copied from the php manual... http://php.net/manual/en/mysqli.select-db.php
Code:
/* attempt and check connection including first database selection "test" */
if (!$mysqli = new mysqli("localhost", "root", "", "test")) {
// never show the actual error to the public
echo "<div>Database Connection Error: " , $conn->connect_error , "</div>";
exit();
}
/* return name of current default database */
if (!$result = $mysqli->query("SELECT DATABASE()")) {
// never show the actual error to the public
echo "<div>Syntax Error: " , $conn->error , "</div>";
} else {
echo "<div>Default database is: " , $result->fetch_row()[0] , "</div>";
$result->close();
}
/* change db to "mysql" db */
$mysqli->select_db("mysql");
/* return name of current default database */
if (!$result = $mysqli->query("SELECT DATABASE()")) {
// never show the actual error to the public
echo "<div>Syntax Error: " , $conn->error , "</div>";
} else {
echo "<div>Default database is: " , $result->fetch_row()[0] , "</div>";
$result->close();
}
$mysqli->close();
Output:
Default database is: test
Default database is: mysql

can not create a database with PHP and my syntax is correct

How is it is can not create a database on phpMyAdmin? I connect to PHP just fine, no error message. But when I am trying to create a database so phpMyAdmin, I keep getting error. In case it matters I have one page with a <form> and has a submit to my second.php file where my database code is (second.php is shown below).
I am pretty much trying to display the database in phpMyAdmin but nothing shows up:
<html>
<body>
<?php
$con = mysqli_connect("localhost", "user", "password");
if(!$con){
echo "could not connect";
} else {
echo "good connection";
}
//creation of database
$sql= 'CREATE DATABASE project';
if(mysql_query($sql,$con)){
echo 'DB created succesfully';
} else {
echo 'error creating DB' . mysql_errno();
}
?>
</body>
</html>
One issue is you connected with mysqli and are using mysql the rest of the time. Pick 1 (not mysql) library and use only that.
if(mysqli_query($con, $sql)){
and
echo 'error creating DB' . mysqli_error($con);

Internal server error while connecting to my database online

I'm using the following code to connect to a database that I have setup on godaddy.com. But when I try to run the script I get "Internal Server Error".
<?php
$db_conx = mysqli_connnect('MY_WEBSITE.secureserver.net', 'MY_USERNAME',
'MY_PASSWORD', 'DATABASE_NAME');
// Evaluate the connection
if (mysqli_connect_errno()) {
echo "cannot connect to database";
echo mysqli_connect_error();
exit();
}
else {
echo "Successful database connection, happy coding!!!";
}
?>
I'm not sure what i'm doing wrong

php connect to mysqlworkbench localhost

I have a mac with OSX 10.8.4. I have installed my localhost and it works just fine. I have made a php script, from where I would like to connect MySQL workbench database to. My apache tomcat server runs, and also mysql on the computer, and I use XAMPP. This is my code:
<?php
// Establish connection to DB using PDO
try {
$pdo = new PDO('127.0.0.1:3306', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
echo "Connected!";
} catch (PDOException $e) {
$error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
echo "Connection failed";
exit();
}
I have tried this script to connect to a remote mysql server, where it works fine, but I cannot use it for my localhost. I also tried just to put in localhost in new PDO, but still the same. Does anybody have a clue to what is wrong?
Best Regards
Mads
You'll have an easier time knowing what's not working if you echo the exception being thrown.
Your code
} catch (PDOException $e) {
$error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
echo "Connection failed";
}
doesn't actually print the exception! Try this instead:
} catch (PDOException $e) {
$error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
echo $error;
}
That will at least give you some helpful debugging info.

PHP-OCI connection

I am new to PHP and am trying to connect to a oracle database on some server.
However the php script is not executing properly.
<?php
echo "started \n";
// Create connection to Oracle
$conn = oci_connect("username", "password", "abc.def.ghi.com");
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!";
}
// Close the Oracle connection
oci_close($conn);
?>
It prints started, but after that it does not print any error or "Connected to Oracle".
php -l filename.php gives "no syntax errors".
Was PHP compiled with oracle support? Check your error log, if not you'll error out on oci_connect() and not reach anything else.
(display errors would also be of help)

Categories