connect oracle database with php and execute sql? - php

Hey guys im trying to connect to an oracle database with php. I tried it like i do it with mysql.
How to do it like this:
$host="localhost";
$user="username";
$pass="password";
$db="database";
$link = mysql_connect($host, $user, $pass) or die ("Keine Verbindung zu der Datenbank moeglich.");
mysql_select_db($db, $link);
$sql = "SQL query goes here";
$result = mysql_query($sql);
How can i do exact this with an oracle database. I have the following connection details
sid, ip, port, username, password.

Simple script:
$DB = '//1.2.3.4:1521/XE';
$DB_USER = 'user';
$DB_PASS = 'pass';
$DB_CHAR = 'AL32UTF8';
$conn = oci_connect($DB_USER, $DB_PASS, $DB, $DB_CHAR);
$statement = oci_parse($conn, 'select 1 from dual');
oci_execute($statement);
$row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS);

To connect with an Oracle database, you don't use the mysql extension (since that is for MySQL). You should use PDO, with the OCI/Oracle adapter.

You'll want to use PDO to connect to Oracle here is the PHP manual page on creating a connection using PDO, the example given is for MySQL but it will work fine with Oracle. You will need to ensure the PDO:Oracle extension is installed and running on your PHP configuration.

Related

can not get mysql INFORMATION_SCHEMA result

help me please guys
I've searched and tried all the articles here
but still can not get results from INFORMATION_SCHEMA
I want to get a relationship between tables using KEY_COLUMN_USAGE but still not showing anything, just displaying a blank page
I do not understand what went wrong:
I've used the code I used to use, as below
$server = "localhost";
$user= "root";
$pass= "";
$db= "my_DB";
$conn = mysql_connect($server, $user, $pass) or die ("can't connect server" .mysql_error());
$openconn= mysql_select_db($db) or die ("can't open DB " .mysql_error());
$sql="using_select_command_here";
$query = mysql_query($sql) or die ("error:" .mysql_error());
while ($data = mysql_fetch_array($query)) {
print_r($data);
}
from all sources are here like :
How to find tables all relation in MySQL
How to know relations between tables
the result still showing blank page:
I do not understand if there are other factors?
I am using php version 5.3.1
and mysql version 5.1.41
from xampp for windows ver1.7.3

PHP Can't Run Query on Microsoft SQL Server

I'm not sure what's wrong with the following code. I'm trying to connect to MSSQL with PHP to run a query. The code can connect to the server and database, but fails on query execution. This code is virtually identical to the way I retrieve data from MySQL. What is the mistake I'm making here?
Thanks.
$link = mssql_connect ($server, $user, $password)
or die ( "<h1>ERROR: Can't connect to MSSQL.</h1>");
$db_selected = mssql_select_db ($db, $link)
or die ("<h1>ERROR: Can't read MSSQL database.</h1>");
$sql = 'SELECT * FROM [uStore].[dbo].[Store]';
$result = mssql_query ($sql)
or die ("<h1>ERROR: Can't execute MSSQL query.</h1>");
echo 'ok';
mssql_free_result ($link);

Connecting several MySQL databases with 1 shared user

I am trying to integrate a wiki, forum, chat and user database all on 1 site. To connect to the database we use:
<?php
$host="localhost.sitename.com";
$user="user_name";
$password="password";
$database="site_database";
$connection = mysql_connect($host,$user,$password)
or Die ("Could not connect to Server.");
$db=mysql_select_db($database,$connection)
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("site_database");
?>
I need it to also connect to the 3 other databases and am wondering if I need to list each one with a separate $database and again mysql_select_db line or all in one with a , in between?
$database="site_database";
$database="chat_database";
$database="wiki_database";
$database="forum_database";
and
mysql_select_db ("site_database");
mysql_select_db ("chat_database");
mysql_select_db ("wiki_database");
mysql_select_db ("forum_database");
OR
$database="site_database","chat_database","wiki_database","forum_database";
and
mysql_select_db ("site_database","chat_database","wiki_database","forum_database");
???
You can only have one database selected per connection. If you want to have 4 databases open and selected at the same time, even if your using the same login credentials you will need to have 4 open connections. Otherwise you will need to select the appropriate database before each MySQL query if the last MySQL query on that connection had a different database selected.
First: You should use mysqli
Second: Try like this:
$Con1 = new mysqli ("host","user","password","database");
$Con2 = new mysqli ("host","user","password","database");
$Con3 = new mysqli ("host","user","password","database");
Then :
$PreparedStatement1 = $Con1->prepare();
$PreparedStatement1->bindparam('',);
$PreparedStatement1->execute();
$PreparedStatemet1->close();
$PreparedStatement2 = $Con2->prepare();
$PreparedStatement2->bindparam('',);
$PreparedStatement2->execute();
$PreparedStatemet2->close();
What you can do is create a function which connects you to the database
function connect($host, $username, $password, $database) {
$db = mysql_connect($host, $username, $password);
mysql_select_db($database, $db);
return $db;
}
This is theorically what you want to do, but here are some tips:
Avoid using mysql extension (you SHOULD use mysqli or PDO)
Try not to switch between databases with a connection for further database compatibility (although MySQL supports database switching, not every DBMS will behave the same)
Also, if you had to perform a couple of operation you could do:
mysql_select_db('first', $db);
// Perform some queries
mysql_select_db('second', $db);
// Perform some other queries
mysql_select_db('first', $db);
// And then switch back to the first database

fixing database encoding to UTF8

I need to change encoding of whole database to UTF8.
I was connect to database like this before:
// MySQL connect information.
$username = "root";
$password = "";
$host = "localhost";
$database = "yac";
// Connect.
$connection = mysql_connect($host, $username, $password)
or die ("It seems this site's database isn't responding.");
mysql_select_db($database)
or
die ("It seems this site's database isn't responding.");
my database content was stored to database without mysql_query("SET NAMES UTF8"); and data was like this:
باشگاه هوانوردان جوان
now recently I connect to database like this:
$mysql_username = "root";
$mysql_password = "";
$mysql_host = "localhost";
$mysql_database = "tick5";
$connection = mysql_connect($mysql_host, $mysql_username, $mysql_password) or die ("It seems this site's database isn't responding.");
mysql_select_db($mysql_database) or die ("It seems this site's database isn't responding.");
mysql_query("SET NAMES utf8");
mysql_query("SET CHARACTER_SET utf8");
and stored data is like this:
عنوان مقاله
since I use mysql_query("SET NAMES UTF8"); before INSERT queries I haven't any problem but now I need my old content and I want to change encoding and fix this.
How to do this?
NOTE : my tables Collation is utf8_unicode_ci
Try to see if this can help you. http://www.gentoo-wiki.info/TIP_Convert_latin1_to_UTF-8_in_MySQL
If you do not have a running Linux system, you can download an Ubuntu LiveCD and run it directly from the CD. http://www.ubuntu.com/download/desktop

server is disconneting database

database get connected successfully....but...
here is my code
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'databasename';
mysql_connect($host, $user, $pass) or die ("Database Not Connected");
mysql_select_db($db) or die ("Database Not Fount");
?>
but the database is regularly disconnecting and connecting after 30-40 minutes....please help me, that what's going on.....
Don't forget to close your connection with mysql_close().
Too many connections will cause problems so that might explain your disconnects.
This may be the problem of confusing variables. Your connection and db-selection shouldn't be confused with queries that will run at a later time.
$conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$query = "SELECT id, username FROM users";
In this example, $conn will not be used to reference anything other than my resource. My query, ran at a later time, will be known as $query, so as to not confuse myself.
I would also suggest watching the execution times of your queries, and the number of concurrent connections opened. If you need to, be sure to close your connections:
mysql_close($conn); // note the importance of a unique variable here

Categories