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.
Related
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
I am trying to throw implement some error handling using Oracle and PHP. If I try to insert the statement into the DB table where the PK already exists, there is no INSERT performed - yet it returns that the data was added - when really it isnt - Need some help with the Error HAndling
require('connection.inc');
require('connection_db.inc');
$conn = db_connect();
$sql = oci_parse($conn,"INSERT INTO Schema.TableA (DOE, Trips) VALUES (:doe, :trp)");
oci_bind_by_name($sql, ':doe', $f1);
oci_bind_by_name($sql, ':trp', $f2);
oci_execute($sql);
<?
if($sql)
{
echo("Input data has been added<br><br>");
echo("<a href='link1.php'>View Links</a>");
}
else
{
echo("Input data has failed");
echo "</div>";
}
?>
You are evaluating the statement identifier $sql and not the result of the oci_execute call ...
oci_execute will return true if successful and false in case the query failed. See http://php.net/manual/en/function.oci-execute.php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
$stid = oci_parse($conn, 'SELECT * FROM employees');
$result = oci_execute($stid);
if(true === $result){
// Query successfully executed
echo "Hooary";
} else {
// Something went wrong
$e = oci_error($stid);
echo "Error: " . $e['message'];
}
Small tip, judging from the code you posted you seem to be in the process of learning php, i'd say take a look at PDO if you want to have a safer and easier way of interacting with your database. There is an oci driver available for PDO.
http://php.net/manual/en/book.pdo.php
http://php.net/manual/en/ref.pdo-oci.php
Hi I know this is a little general but its something I cant seem to work out by reading online.
Im trying to connnect to a database using php / mysqli using a wamp server and a database which is local host on php admin.
No matter what I try i keep getting the error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given when i try to output the contents of the database.
the code im using is:
if (isset($_POST["submit"]))
{
$con = mysqli_connect("localhost");
if ($con == true)
{
echo "Database connection established";
}
else
{
die("Unable to connect to database");
}
$result = mysqli_query($con,"SELECT *");
while($row = mysqli_fetch_array($result))
{
echo $row['login'];
}
}
I will be good if you have a look at the standard mysqli_connect here
I will dont seem to see where you have selected any data base before attempting to dump it contents.
<?php
//set up basic connection :
$con = mysqli_connect("host","user","passw","db") or die("Error " . mysqli_error($con));
?>
Following this basic standard will also help you know where prob is.
you have to select from table . or mysqli dont know what table are you selecting from.
change this
$result = mysqli_query($con,"SELECT *");
to
$result = mysqli_query($con,"SELECT * FROM table_name ");
table_name is the name of your table
and your connection is tottally wrong.
use this
$con = mysqli_connect("hostname","username","password","database_name");
you have to learn here how to connect and use mysqli
Can anyone see what the problem with my code is / where im going wrong?
I know i have the correct host,database,user and password.
This is the code in the php file, it should get all the details available on the players from my sql database, however if i go on the page it just gives me a white page. Im using go daddy as a host and my database is also on there.
Any ideas? thanks
<?php
$host = "abc12345"; //Your database host server
$db = "abc12345"; //Your database name
$user = "abc12345"; //Your database user
$pass = "abc12345"; //Your password
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if (!$connection) {
die("Database server connection failed.");
} else {
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Check to see if we could select the database
if (!$dbconnect) {
die("Unable to connect to the specified database!");
} else {
$query = "SELECT * FROM Player";
$resultset = mysql_query($query);
$records = array();
//Loop through all our records and add them to our array
while ($r = mysql_fetch_assoc($resultset)) {
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
?>
The script is all right, I checked it with a different query.
Assuming that the table Player is not empty, the error can be either with your raw sql query (as pointed by #Sharikov in comments), otherwise the error is with the way you have configured your godaddy server.
For debugging that, I would suggest inserting dummy print_r or echo statements before you execute the query, and going through your apache logs at /var/log/apache2/access.log.
Also make sure that you don't have any core php package missing on your server (like php5-mysql if you use mysql).
Newbie question about PostgreSQL. I'm migrating a MySQL/PHP app I've created, hosted on a Linux server, to PostgreSQL/PHP on a MacOSX Lion Server environment. It's my first experience with Postgres. The first query I'm testing doesn't work as it returns nothing (not even an error message, whichever check code I add). What did I do wrong? I've read articles on the web including the doc on php official website but all comments, personal methods and differences from version to version, either with Postgres or PHP, make it very confusing and I eventually don't understand exactly show I should write my query and fetch_array. Thanks for any suggestions.
Here is my code from the original MySQL application:
// below is the "connexion.php" file
function connexion ()
{
$link=#mysql_connect ("localhost","username","pwd");
if ($link && mysql_select_db ("database"))
return ($link);
return (FALSE);
}
// below is the "index.php" file
require ("connexion.php");
connexion() or exit();
$reqcount = mysql_query ("SELECT * FROM people");
$result = mysql_num_rows($reqcount);
echo "Total : ".$result." people";
mysql_free_result ($reqcount);
mysql_query("set names 'utf8'");
$reqcat = mysql_query ("SELECT catname FROM categories ORDER BY catname");
while ($fieldcat = mysql_fetch_array($reqcat))
{
$name = $fieldcat[catname];
echo $name."<br>";
}
mysql_free_result ($reqcat);
mysql_close ();
And here is the PostgreSQL adaptation:
// connexion.php
function connexion ()
{
$link=pg_connect("host=localhost port=5432 dbname=database user=username password=pwd connect_timeout=5 options='--client_encoding=UTF8'");
return ($link);
}
// index.php
require ("connexion.php");
$reqcount = pg_query ($link,"SELECT * FROM people");
$result = pg_num_rows($reqcount);
echo "Total : ".$result." people";
pg_free_result ($reqcount);
$reqcat = pg_query ($link,"SELECT catname FROM categories ORDER BY catname");
while ($fieldcat = pg_fetch_array($reqcat))
{
$name = $fieldcat[catname];
echo $name."<br>";
}
pg_free_result ($reqcat);
pg_close ();
The php code for postgresql doesn't call connexion() so it never connects, unlike the mysql code.
You can try your query if it fails display error
$reqcount = pg_query ($link,"SELECT catname FROM people") or die(pg_last_error());
This way you can see your error.
errors
UPDATE:
Remove the # in your connection to see the error, and add or die(pg_last_error())
to see the error