Oracle Query not Successfully Run on PHP - php

Hi guys I have played around with Oracle SQL and PHP for only a short while so please forgive me if this sounds rookie.
I got this query in my PHP codes to join 4 tables together, here's the query:
SELECT P.PTY_ID,
P.PTY_UNITNUM,
P.PTY_STREET,
P.PTY_POSTCODE,
P.PTY_SUBURB,
P.PTY_CITY,
T.P_TYPE_NAME,
L.LIST_PRICE,
L.SALE_PRICE,
C.SELLER_FNAME,
C.SELLER_LNAME,
L.AVAILABILITY
FROM PROPERTY_TYPE T,
PROPERTY P,
CUSTOMER C,
LISTINGS L
WHERE P.P_TYPE_ID = T.P_TYPE_ID(+)
AND L.SELLER_ID = C.SELLER_ID(+)
AND L.PTY_ID = P.PTY_ID(+)
ORDER BY P.PTY_ID;
In nutshell what I want to achieve is to show the property details, listings information (prices, availability) and customer name all together in a single table. The query works perfectly in SQL developer and gave me the results I wanted, but it is not working in my PHP file and gave me no results but blank.
In order to debug I used another set of data and put in and it worked fine as well. Therefore I can tell the PHP codes are cool and go back to the query. But I can't find any problem in it. Can anyone help me? Did I use the join statements wrongly?
I just add my PHP code below in case you might be interested in it:
<!DOCTYPE html>
<html>
<head><title>Update/Delete Property</title></head>
<body>
<center><h2>Property Records</h2></center>
<?php
$conn = oci_connect("username","password","database");
$query = "SELECT P.PTY_ID, P.PTY_UNITNUM, P.PTY_STREET, P.PTY_POSTCODE, P.PTY_SUBURB, P.PTY_CITY, T.P_TYPE_NAME,
L.LIST_PRICE, L.SALE_PRICE,
U.SELLER_FNAME, U.SELLER_LNAME,
L.AVAILABILITY
FROM PROPERTY_TYPE T, PROPERTY P, CUSTOMER C, LISTINGS L
WHERE P.P_TYPE_ID = T.P_TYPE_ID(+)
AND L.SELLER_ID = U.SELLER_ID(+)
AND L.PTY_ID = P.PTY_ID(+)
ORDER BY P.PTY_ID";
$stmt = oci_parse($conn,$query);
oci_execute($stmt);
?>
<table border="2" align="center">
<tr>
<th><b>Property ID</th>
<th><b>Unit Number</th>
<th><b>Street</th>
<th><b>Postcode</th>
<th><b>Suburb</th>
<th><b>City</th>
<th><b>Property Type</th>
<th><b>Listing Price</th>
<th><b>Sale Price</th>
<th colspan = '2'><b>Seller Name</th>
<th><b>Availability</th>
<th><b>Checkbox</th>
</tr>
<?php
while ($row = oci_fetch_array($stmt))
{
?>
<tr>
<td><?php echo oci_result($stmt,"PTY_ID");?></td>
<td><?php echo oci_result($stmt,"PTY_UNITNUM");?></td>
<td><?php echo oci_result($stmt,"PTY_STREET");?></td>
<td><?php echo oci_result($stmt,"PTY_POSTCODE");?></td>
<td><?php echo oci_result($stmt,"PTY_SUBURB");?></td>
<td><?php echo oci_result($stmt,"PTY_CITY");?></td>
<td><?php echo oci_result($stmt,"P_TYPE_NAME");?></td>
<td><?php echo oci_result($stmt,"LIST_PRICE");?></td>
<td><?php echo oci_result($stmt,"SALE_PRICE");?></td>
<td><?php echo oci_result($stmt,"SELLER_FNAME");?></td>
<td><?php echo oci_result($stmt,"SELLER_LNAME");?></td>
<td><?php echo oci_result($stmt,"AVAILABILITY");?></td>
</tr>
<?php
}
oci_free_statement($stmt);
oci_close($conn);
?>
</table>
</body>
</html>
The query in this PHP code is similar so I did not change it. It was not running properly as well.
NEW PROGRESS..
So I tried to change one line of the code:
AND P.PTY_ID = L.PTY_ID(+)
I started to get something...But still not what I wanted:
New result
I am pretty sure it's about the query now and it's something about the connection between entities. SO again I am sending up my ERD here, which might help (focus on table LISTINGS, SELLER (which is called CUSTOMER now) and PROPERTY):
ERD of the entities

Related

Using links in HTML tables associated with the right table row/person/id in the database

Im a begginer in the whole SO and programming enviroment, and here comes my first question(which i tried finding in here but with no luck so far..)
I have an html table that prints specific patients of the doctor that is active (in a simple web-application that i created). The doctor gets filtered using $_SESSIONS global var in php.
My problem is that i want to implement a few actions in the same HTML table that displays the patients, like view history (which is stored in a local DB table, from an HTML from using POST method) and create a new form for the same person.
I saw that providing the table with row.ids could be a solution, but my table isn't static, i woulda like for the user to have the option to add/delete patients..
Following is a sample of my code that displays the HTML table "Existing Patients" :
<table>
<tr>
<th>Patient Id</th><th>Patient Name</th><th>Phone Number</th><th>Email</th><th>History</th>
<th>Add a Follow Up Visit</th><th>Remove Patient</th>
</tr>
<?php $sql = "SELECT * FROM patients WHERE Doctor_ID = $usersid";
$result = $pdo->query($sql);
if($result->rowCount() > 0){
while($row = $result->fetch()){?>
<tr>
<td><?php echo $row['Patient_id']; ?></td>
<td><?php echo $row['Patient_name'] ; ?></td>
<td><?php echo $row['Phonenum'] ; ?></td>
<td><?php echo $row['Email']; ?></td>
<td><?php echo "<a href='/patienthistory.php'</a>" . 'Previous Visits'; ?></td> //problem here
<td><?php echo "<a href='/patientform.html'</a>" . 'Add Follow Up' ; }?></td> //and here
</tr>
</table>
I want the last 2 lines to connect immediately to the specific database stored Patient_id and retrieve the existing information stored there from the patients previous visits.
I hope i gave enough of a description, but if there is any more info neccesary, please let me know.
Thank you in advance!
You can specify the patient ID as a query parameter in the URL that you're linking to. Also the HTML you're generating for your links is invalid currently. And the second link is going to need to go to a .php script, not a .html file, if you want it to execute code and fetch data.
Try this example:
<td><?php echo "<a href='/patienthistory.php?id='".$row['Patient_id']."'>Previous Visits</a>"; ?></td>
<td><?php echo "<a href='/patientform.php?id='".$row['Patient_id']."'>Add Follow up</a>"; ?></td>
Then in each of the PHP scripts, use $_GET["id"] to retrieve that ID of the patient and use that in a query
e.g.
$patientID = $_GET["id"];

Handling loops and combining values in PHP

I have a table like this.
author_id author_state author_name
1 CA Hello
2 MI World
3 CA How
4 MI Are
Please let me know how can I achieve this. In other words, I should loop it and combine the state values first and display the details.
Here is my PHP Code
<table>
<tr><td>Author ID</td><td>Author State</td><td>Author Name</td></tr>
<?php
$row_data = mysql_query("select * from author" );
while($row = mysql_fetch_array($row_data) ) {
?>
<tr>
<td><?php echo $row['author_id']; ?></td>
<td><?php echo $row['author_state']; ?></td>
<td><?php echo $row['author_name']; ?></td>
</tr>
<?php } ?>
</table>
My Expected output is to combine the state and display something like below:
Author ID Author Name
=======================================
CA
==
1 Hello
3 How
MI
==
2 World
4 Are
You can do this fairly easily, by sorting your output by author_state, and using a variable to keep track of which state you're outputting. If the state changes, then you simply output the state as a new header.
You can take advantage of the SQL order by clause so that you don't have to sort the data yourself.
Here is an edit of your code, where I've simply added the order by to your mysql_query(), and added a variable for $current_state.
<table>
<tr><td>Author ID</td><td>Author State</td><td>Author Name</td></tr>
<?php
$current_state = "";
$row_data = mysql_query("select * from author order by author_state" );
while($row = mysql_fetch_array($row_data) ) {
// Output the "new" state
if ($row['author_state'] != $current_state)
{
echo "<tr><td colspan=\"3\">{$row['author_state'}}</td></tr>";
$current_state = $row['author_state'];
}
?>
<tr>
<td><?php echo $row['author_id']; ?></td>
<td><?php echo $row['author_state']; ?></td>
<td><?php echo $row['author_name']; ?></td>
</tr>
<?php } ?>
</table>
As a side note, I would strongly encourage you to look into using mysqli or PDO instead of the mysql_* functions. The mysql_* functions are only availble in very old versions of PHP (they were completely removed in PHP7).

Table data is not arranged

as i am a beginner so i want to ask how can i run the multiple queries using PHP for the same row to have multiple data in the table in html. i have tried many attempts but nothing worked i am using MYSQL as backend. I know that i have not written code for the first row and second column i.e. the second <td> on wicket keeper but the thing is that i want all rounder data to come in the second <td> of wicket keeper section. when running this code it is providing the output fine for wicket keeper but for all rounder column the data is starting after the end of wicket keeper data. i know why this problem is occurred but how can i solve this please tell??
CODE HERE:
<?php
$sql="select * from teams where role = 'WicketKeeper'";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
while($row=mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row["name"] ?></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php } ?>
<?php
$sql="select * from teams where role = 'AllRounder'";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
while($row=mysqli_fetch_assoc($result)){
?>
<tr>
<td></td>
<td><?php echo $row["name"] ?></td>
<td></td>
<td></td>
</tr>
<?php } ?>
Here is a good way to generate the rows of the display you showed.
First, use this query:
SELECT name, role FROM teams ORDER BY role DESC;
It generates rows in the order you want them, wicketkeepers first.
Then, use this php code to generate your display.
<?php
$sql="SELECT name, role FROM teams ORDER BY role DESC";
$result=mysqli_query($conn,$sql) or die(mysqli_error());
while($row=mysqli_fetch_assoc($result)){
$role = $row["role"];
$name = $row["name"];
$wicketkeeper = $role == "WicketKeeper" ? $name : "";
$allrounder = $role == "AllRounder" ? $name : "";
?>
<tr>
<td><?php echo $wicketkeeper ?></td>
<td><?php echo $allrounder ?></td>
<td></td>
<td></td>
</tr>
<?php } ?>
This renders the rows in the result set from your query, placing names in columns according to their roles.
In the general case, this task is known as pivoting a table.
Pro tip 1 Try to avoid SELECT * in queries. Instead give the names of the columns you need. This makes your code easier for a stranger to read, and it may make your queries run faster.
Pro tip 2 Anytime you catch yourself reusing the same query where it only varies by a WHERE clause, try to use a single query. This can make your application run faster.

Making a HTML table based on SQL data in PHP

I am trying to make a dynamic HTML table with PHP, populating it with data from MySQL database. So far I have tried the while loop, but the result ends up in displaying the same first row it gets multiple times.
<div class = "container">
<p>Registered companies:</p>
<table border = "1px" align = "left">
<tr>
<th>Username</th>
<th>Company name</th>
<th>Company value1</th>
<th>Company value2</th>
</tr>
<?php
$compRowIncrement = 0;
while ($compRowIncrement < $companyRowCount) {
?>
<tr>
<td><?php echo $companyRow['user_name']?></td>
<td><?php echo $companyRow['company_name']?></td>
<td><?php echo $companyRow['company_value1']?></td>
<td><?php echo $companyRow['company_value2']?></td>
</tr>
<?php
$compRowIncrement++;
}
?>
</table>
</div>
It should display 3 rows of data for example (SQL query returns 3 different values). But so far I have achieved to get 3 rows (like I need) with the same data (first value it gets from the database).
How do I do it so each table row is populated with different data, as it is in the database.
I'm just learning, so if you don't mind ignore the css values in table :).
EDIT1 (Added query)//
$getPlayerCompanies = $MySQLi_CON -> query("SELECT DISTINCT *
FROM companies
LEFT JOIN player ON companies.player_id = player.player_id
LEFT JOIN users ON users.user_id = player.user_id
WHERE users.user_id =".$_SESSION['userSession']);
$companyRow = $getPlayerCompanies -> fetch_array();
$companyRowCount = $getPlayerCompanies -> num_rows;
Following query currently returns 3 rows, like it should.
where did the $companyRow values get populated?
I belive your code should be more like this (or with mysqli commands)
<?php
while ($companyRow = $getPlayerCompanies -> fetch_array() )
{
?>
<tr>
<td><?php echo $companyRow['user_name']?></td>
...
</tr>
<?php
}
?>

Trying to return a record set using PHP & MySQL (inner join)

I am new to PHP/MySQL to please bear with me. I am trying to have PHP write a table which returns a list of records from a join table. The SQL statement works perfectly when I run the query but I do not know how to write the function properly.
SQL statement which works:
SELECT members.nick_name, assets.asset_desc, shares.asset_cost, shares.percent_owner
FROM
(shares INNER JOIN assets
ON shares.asset_ID = assets.asset_ID)
INNER JOIN members
ON shares.member_ID = members.member_ID
WHERE shares.member_ID = $member_ID"
My functions:
function get_shares_by_member($member_ID) {
global $db;
$query = "SELECT members.nick_name, assets.asset_desc, shares.asset_cost, shares.percent_owner
FROM
(shares INNER JOIN assets
ON shares.asset_ID = assets.asset_ID)
INNER JOIN members
ON shares.member_ID = members.member_ID
WHERE shares.member_ID = $member_ID";
$share_result = $db->query($query);
$share_result = $share_result->fetch();
return $share_result;
}
function get_shares() {
global $db;
$query = "SELECT * FROM shares";
$share = $db->query($query);
$shares_table = $share->fetch();
return $share;
}
My action:
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_shares';
}
if ($action == 'list_shares') {
if (!isset($member_ID)) {
$member_ID = 0;
}
$shares = get_shares_by_member($member_ID);
$share = get_shares();
}
Here is my table:
<table>
<tr>
<th>Nick Name</th>
<th>Asset Description</th>
<th>Asset Cost</th>
<th class="right">% Ownership<th>
<th> </th>
</tr>
<?php foreach ($shares_table as $share) : ?>
<tr>
<td><?php echo $share['nick_name']; ?></td>
<td><?php echo $share['asset_desc']; ?></td>
<td><?php echo $share['asset_cost']; ?></td>
<td class="right"><?php echo $share['percent_owner']; ?></td>
<td> </td>
</tr>
<?php endforeach; ?>
</table>
I know this is a lot to ask but I've been struggling with this for the past 3 days. Any help will be much appreciated! If anyone needs help with AD/Exchange, I'd be happy to share my knowledge in that area!
Thanks!!
From what I can see on here, this will produce a blank table with however many rows are returned for a number of reasons:
None of the columns returned by the get_shares_by_member function are share_ID or asset_ID, so those columns won't be filled in.
You are referencing percent_owner from $shares which, assuming is an array as you are using it in a 'foreach' loop, will need an index to reference it, or it should otherwise be $share['percent_owner']
The percent_owner field will be put in the 'asset cost' column at present as there is no blank cell produced to move it to the '% ownership' column where it would seem logical to have it.
Based on what you have posted so far, the following should suit your needs:
<table>
<tr>
<th>Nick Name</th>
<th>Asset Description</th>
<th>Asset Cost</th>
<th class="right">% Ownership<th>
<th> </th>
</tr>
<?php foreach ($shares as $share) : ?>
<tr>
<td><?php echo $share['nick_name']; ?></td>
<td><?php echo $share['asset_desc']; ?></td>
<td><?php echo $share['asset_cost']; ?></td>
<td class="right"><?php echo $shares['percent_owner']; ?></td>
<td> </td>
</tr>
<?php endforeach; ?>
</table>
I would recommend changing either the $share variable in the foreach, or the $share which is being set by get_shares(). Personally speaking, I would change the latter from
$share = get_shares();
to something like:
$shares_table = get_shares();
as it is essentially containing all of the information from the shares table, assuming the database abstraction layer function fetch() returns all results.
There could also be an issue when you are doing:
$share = $db->query($query);
$share = $share->fetch();
Going from different database abstraction layers I have seen, I would expect fetch() to be done as (using your variables)
$share = $db->fetch();
If the fetch() is requiring a result to be passed into it, then I would expect the code to look similar to:
$share_result = $db->query($query);
$share = $db->fetch($share_result);
a few points:
are you sure your query does not return any errors? If there are
errors, that might cause fetch() to fail
what DB class are you using? I would suggest that you please check
that there is a fetch() function and what parameters does it accept? For example, the fetch() may be invoked like $share_result->fetch() or $db->fetch() or $db->fetch($share_result) etc.
I might be wrong but it seems that the fetch() might be always
returning the first row from the resultset. Perhaps you might need
to do fetch() in a loop for reading all results
You may as well try using the default PHP functions. Here is a code snippet that explains how you may rewrite your functions using PHP's default mysql() library:
mysql_connect('your host', 'your user', 'your password'); function get_shares_by_member($member_ID) {
$output = Array();
$query = "SELECT members.nick_name, assets.asset_desc, shares.asset_cost, shares.percent_owner
FROM
(shares INNER JOIN assets ON shares.asset_ID = assets.asset_ID)
INNER JOIN members ON shares.member_ID = members.member_ID
WHERE shares.member_ID = $member_ID";
$share_result = mysql_query($query);
while ($row = mysql_fetch_assoc($share_result)) {
$output[] = $row;
}
return $output;
}
function get_shares() {
$output = Array();
$query = "SELECT * FROM shares";
$share = mysql_query($query);
while ($row = mysql_fetch_assoc($share)) {
$output[] = $row;
}
return $output;
}
Hope the above helps. Please feel free to let me know if there is anything that is not clear.

Categories