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)
Related
It's been many years since I did any dev in PHP.
The concept of connecting to MySQL using PDO is completely new to me, and I don't seem to be able to get it to work.
Using MySQLi, and the following code, which I know is deprecated, works nicely:
<?php
include 'models/db_details.php';
$db_connection = mysql_pconnect("$dbhost", "$dbusername", "$dbpasswd") or die("Couldn't connect to server: " . mysql_error());
$db = mysql_select_db("$dbname", $db_connection) or die("Couldn't select database.");
?>
<h1>After connecting to the DB</h1>
As expected, this displays:
After connecting to the DB
From all of the pages I have read, using PDO, this should look something like:
<?php
include 'models/db_details.php';
try
{
$conn = new PD0("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbusername, $dbpasswd);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
<h1>After connecting to the DB</h1>
The result of this is a completely blank page.
All of this is on a FreeBSD box, running:
Apache/2.4.25
PHP/5.6.30
MySQL 5.6.35
I have uncommented extension=php_pdo_mysql.dll in php.ini.
I can also confirm that the MySQL driver for PDO is installed:
What am I missing?
Check the font. Make sure it is pdo and not pd0 (zero).
Magic happens after that.
I am using this php to test if I am connected in a postgree database, works very well, but how can I insert a error Message and a Message showing the database is connected and not connected?
Example:
like: You are connect to:database_name
or:
You could not connect to:database_name
That is my code:
<?php
$connection = pg_connect ("host=localhost dbname=site user=postgres password=root");
?>
Just test the truthiness of the connection:
<?php
$connection = pg_connect ("host=localhost dbname=site user=postgres password=root");
if($connection) {
echo 'connected';
} else {
echo 'there has been an error connecting';
}
?>
Return value of pg_connect() is
PostgreSQL connection resource on success, FALSE on failure.
so check this value:
if (!$connection = pg_connect ("host=localhost dbname=site user=postgres password=root")) {
$error = error_get_last();
echo "Connection failed. Error was: ". $error['message']. "\n";
} else {
echo "Connection succesful.\n";
}
I'm trying to connect php with mysql so I write the code like this:
<?php
// Create connection
echo "1";
$con=mysqli_connect("localhost","root","pwd");
echo "2";
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
but i dont get any response, there is only a "1" on the page. I'm sure that the uname and pwd is correct. Any advices?
Use mysqli_connect function instead of mysql_connect,
<?php
// Create connection
$con=mysqli_connect("example.com","root","pwd","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Seems like the mysqli extension not enabled in your php.ini configuration.
After enabling the extension restart the Apache server and try again the running the code.
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>';
I have a problem with my database! Here is my code:
<?php
$host = "/homes/49/jc192699/public_html/dbase";
$database = "EduPro.db";
$dbhandle = new PDO("sqlite:".$host.$database);
if (!$dbhandle){
echo "Error connecting to database.\n";
}
else{
echo "<br>";
echo "<br>";
echo "Database connection successful!";
}
mysql_select_db($database);
?>
The problem is that it's saying "Database connection successful!" No matter what I do, if I type the address in wrong, it still says successful, when I renamed the database to a database that doesn't exist, it still says successful. I can't see what the problem is here?
If anybody could help me out it would be GREATLY appreciated!
Thank you!
For starters, the PDO constructor throws an exception if there is an error. It does not return false. Check for errors using
try {
$dbhandle = new PDO("sqlite:".$host.$database);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Secondly, as you are using SQLite, provided your dbase directory is writeable by the script, your connection attempt will create an empty database.
Try this:
<?php
try {
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:/path/to/database.sdb");
}
catch(PDOException $e)
{
/*** real error message prints here ***/
echo $e->getMessage();
}
?>
This is directly taken from here:
http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#4.2