Connection to a Oracle Database using session_start - php

I'm a IT-Apprentice in Switzerland currently working on a project for work. I'm very new to PHP so there may be some unclear points which I will address when they arise. I didn't find anything helpful on Google.
Onto the project.
I have to be able to connect to a Oracle SQL Database with PHP. On this "Webpage" I will have to Insert, Update and Delete Data from a existing Oracle SQL Database.
The way users will authenticate themselves is by just entering there Username and Password. The database host name and service name should be saved in the php file as it will always be the same. There are different users with different rights.
I'm wondering how I can get a session running until the user disconnects from it.
An example of how the user navigates through the webpage is as follows:
Login (Users login with there Oracle Database Accounts) -> Search (search through various tables in the database) -> Results (able to select a result) -> Edit (able to edit the found data and save it) -> Logout/Back(able to logout or go back to the search results).
I've accomplished a connection to the Database using the following code:
<!DOCTYPE html>
<html>
<head>
<title>DB Connection</title>
</head>
<body>
<?php
// Connects to the XE service (i.e. database) on the "localhost" machine
$conn = oci_connect('username', 'password', '//hostname:port/servicename');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, 'select column_name1, column_name2, column_name3 from table_name1 WHERE ROWNUM <= 20');
oci_execute($stid);
echo "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
How can I extract the connection info to a Login page and give over all the necessary data so that I can Log in and navigate through the website without a problem?
If there are any Questions (there probably will be) please ask them. I'll do my best to answer them with the knowledge I have.
Thank you very much
Ben

Related

Oracle selection through php won't show on my page

I'm having some problem with accessing Oracle database records through PHP - OCI8 functions
So I am trying to echo any record in PHP from my Oracle database to my page.
<?php
$conn = oci_connect('SYSTEM', '1234', 'localhost/orcl');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}else{
echo "Successfully connected!";
}
$sql = 'SELECT * FROM userPage';
$stid = oci_parse($conn, $sql);
oci_execute($stid);
while (($row = oci_fetch_array($stid, OCI_BOTH)) != false) {
echo $row[0];
echo $row[1];
}
oci_free_statement($stid);
oci_close($conn);
?>
I am not getting any error back so my connection is valid and also my query is working well on my table because if I change the name of the table in the query I am getting an error that my table does not exist.
I have almost tried everything from oci8 functions and all my result is a blank page.
In Oracle I have created the table correctly and trying to access the data through SYSTEM so I should't have privilige problems.
I am using Oracle 12c, instant client 12_2 windows 7 operating system (didn't work on win10 at all), XAMPP v3.2.4.
Any ideas would help a lot. Thanks for reading.

How to store <li>-item created by user server sided (mySQL)

I have a simple <ul> where you can add a list item with an input field:
$('#plannerform').submit(function(e) {
var val = $(this).find('#plannername').val();
$('ul.plannerlist:visible').append('<li>' + val + '</li>');
e.preventDefault();
});
My question is: Is it possible in a way to save this created list on a mySQL database on a webserver? I haven't got much experience in PHP or other languages for server sided storage. And if it's possible, can you tell me any links where it's explained how? I spent some time already with searching, but I didn't find anything because I simply don't know what to search after.
Here is a simple php script that I wrote to connect to my MySQL DB, and retrieve along with display each result in a given column.
Just replace:
Hostname with probably your local IP if MySQL is installed on your local machine.
Your MySQL username, probably root if you haven't created another user.
The password, which could be blank if you didn't create one.
Database with the name of your Database.
<?php
//Connect to DB
$conn = new mysqli("Hostname","Username","Password","Database");
//If the connection has errors
if ($conn->connect_error){
//Display the error
die("Connection failed because: " . $conn->connect_error);
}
//Otherwise the connection is good so lets create a sql query
$sql = "SELECT * FROM Database";
//Get the results of the query
$result = $conn->query($sql);
//If the results have rows
if($result->num_rows > 0){
//While there are more rows in the results
while($row = $result->fetch_assoc()) {
//Display in HTML paragraph form: Column1 = DataInColumn1
echo '<p> Column1 = ' . $row["Column1"] . ' </p>';
}//End While
}//End If
//Otherwise the query had no results
else{
echo '<p>No results found!</p>';
}
?>

Library List is not being used in remote PHP DB2 connection to IBM i

I've successfully connected to a remote IBM i DB2 database (AS400) from my local Windows PC via PHP. I'm using the IBM Data Server Client in conjunction with the db2_* functions in PHP. The problem I'm having is that despite my library list being set properly, it is not being used for unqualified table names. Instead it uses the current user name as the library. However, when I qualify the table names everything works like a charm.
I've confirmed that my library list is actually changing when I create the connection by querying QSYS2.LIBRARY_LIST_INFO.
$database = '<database name>';
$user = '<user name>';
$password = '<password';
$port = <port>;
$options['i5_naming'] = DB2_I5_NAMING_ON;
$options['autocommit'] = DB2_AUTOCOMMIT_OFF;
$options['i5_libl'] = 'MYLIB YOURLIB ANYLIB';
$conn = db2_connect($database, $user, $password, $options);
if ($conn) {
echo "Connection succeeded."; //It succeeds
}
else {
echo db2_conn_error()." | ".db2_conn_errormsg()."<br />";
echo "Connection failed.";
}
$sql = "SELECT * FROM QSYS2.LIBRARY_LIST_INFO";
//Works and proves my library list reflects
//what I passed in when creating the connection.
//$sql = "SELECT * FROM LIBRARY_LIST_INFO";
//Generates: "42S02 : [IBM][CLI Driver][AS] SQL0204N "<user name>.LIBRARY_LIST_INFO" is an undefined name. SQLSTATE=42704 SQLCODE=-204"
//where <user name> is the username used to connect to the DB.
//It should be using the library list specified when creating the connection though.
//This holds true for any table from any library including those specified
//when creating the connection (which includes QSYS2).
$stmt = db2_prepare($conn, $sql);
$result = db2_execute($stmt);
if($result){
while($row = db2_fetch_assoc($stmt)){
echo "<pre>";
var_dump($row); //In addition to entries for QSYS, QSYS2, QUSRSYS and QHLPSYS I get entries for MYLIB, YOURLIB and ANYLIB.
echo "</pre>";
}
}else{
echo "failed<br />";
echo db2_stmt_error()." : ".db2_stmt_errormsg()."<br />";
}
Has anyone ever run into this while enabling i5_naming when connecting to a remote DB2 server? I'm not really sure why it wouldn't be using my library list as the PHP manual states "Unqualified files are resolved using the library list for the job." when enabled. http://php.net/manual/en/function.db2-connect.php
I finally solved this after opening a PMR with IBM. All I had to do was apply the latest Fix Pack for DB2 Connect Personal Edition.
Suggested Fix Packs for DB2 Connect:
http://www-01.ibm.com/support/docview.wss?rs=71&uid=swg21321001
Basically the DB2 Connect version I had was released prior to 2013. It was in 2013 IBM added two tier support by adding the i5_naming option. So my DB2 Connect setup was effectively ignoring the option I was passing. So that explains why the other options still went through. On the DB side, since it didn't receive a value for i5_naming - it remained as the default.

tables not being created in database

Ok, I'm following a youtube guide on how to create a very simple blogging system using PHP/MySQL as I'd like to get to learn these 2 languages a bit more. I'm creating this in my localhost, permissions set-up correctly.
The problem is, when I go onto localhost/tables.php, it comes up as white screen which it's supposed to, but it's not creating the relevant tables within the database?
Here's the code I'm using:
mysql.php
<?php
mysql_connect('localhost','username','password'); //where localhost is the host, username is the relevant username and password is the relevant password.
mysql_select_db('database'); //where database is the chosen database in which to drop the tables.
?>
tables.php
<?php
include "mysql.php";
$table = "ENTRIES";
mysql_query ("CREATE TABLE IF NOT EXISTS `$table` (`ID` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY ( `id` ) )");
mysql_query ("ALTER TABLE `$table` ADD `TITLE` TEXT NOT NULL");
mysql_query ("ALTER TABLE `$table` ADD `SUMMARY` TEXT NOT NULL");
mysql_query ("ALTER TABLE `$table` ADD `CONTENT` TEXT NOT NULL");
?>
Nothing is appearing in the error log which is frustrating and not helping me diagnose the problem.
Any help would be much appreciated! Thanks.
As you have no errors run this code to show if you have created created the table ENTRIES.
<?php
include "mysql.php";
$result = mysql_query("SHOW COLUMNS FROM `ENTRIES`");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
}
?>
NOTE As you are starting with MySQL you would be advised to use PDO in place of the deprecated mysql_.
Here is a good tutorial
EDIT
Following comments the following code lists databases and tables on server(Note uses deprecated mysql_ function).
Ensure that the proper parameters replace "XXX".
<?php
$host= "localhost";
$username="XXX";
$password="XXX";
$database="XXX";
$link = mysql_connect($host,$username,$password); //where localhost is the host, username is the relevant username and password is the relevant password.
$db_list = mysql_list_dbs($link);
while ($row = mysql_fetch_object($db_list)) {
echo $row->Database . "\n";
echo "<BR>";
}
echo "<BR>";
$result = mysql_list_tables($database);
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < $num_rows; $i++) {
echo "Table: ", mysql_tablename($result, $i), "\n";
echo "<BR>";
}
?>
Nothing is appearing in the error log which is frustrating and not helping me diagnose the problem
So your first poblem is to find out why it's not logging any errors. BTW it would also be a good idea to inject some echo / print statements to find out where it's failing.
Forget about the MySQL stuff and try:
<?php
trigger_error("Testing", E_USER_WARNING);
?>
Even though (once you've got the error reporting sorted out) you should get an error logged it will likely only contain a limited amount of information. Any time you call a mysql function, be via mysql_, mysqli or PDO, you should poll the return value and handle any error - even if it's just to echo the value to the screen.
It's saying that I haven't selected a database
Possibly the user account you are connecting as does not have permission to access the database.

PHP cannot find tables even after successfully connecting to the database

The below code re-creates the issue I can't get around. I just can't seem to figure out where the problem lies - in the code? MySQL settings? or somewhere else? Any pointers in the right direction will be appreciated.
<html>
<head></head>
<body>
<?php
$db_name = "UserDB";
$open = mysql_connect("localhost", "root", "");
if($open)
echo "1. Successfully connected to MySQL";
echo "</br>";
$db = mysql_select_db($db_name, $open);
if($db)
echo "2. Successfully selected {$db_name} database";
echo "</br>";
$sql = "SHOW TABLES FROM `{$db_name}`";
$result = mysql_query($sql);
$print = mysql_num_rows($result);
if($result)
echo "3. {$print} tables found in {$db_name}";
?>
</body>
</html>
Here's my output:
1. Successfully connected to MySQL
2. Successfully selected UserDB database
3. 0 tables found in UserDB
The problem lies in line 3 of the output. It says "0" tables, which is incorrect. I have created "3" InnoDB tables in the selected DB. If I copy/paste and run the same SHOW TABLES query in phpmyadmin, it runs perfectly.
Any idea what is going on here??
Try using the wrapper function mysql_list_tables. I found it impossible once to use the SHOW TABLES due to some weird permissions definition, though I could use mysql_list_tables.

Categories