Trying to get property of non-object using OCI connection - php

I'm new in PHP with OCI connection. I want to retrieve some data from database and insert it into a table form. But it keep show an error
Trying to get property 'attribute' of non-object
I have try to use oci_fetch object / oci_fetch_array, but it still the same. I also have followed some tutorial but it doesn't help.
This is for my mini project for this semester.
Here my source code:
$sql="SELECT borrow.book_id, book_title, borrow.stud_id, stud_name, book_bdate, return_date, due_date
FROM book
JOIN borrow
ON book.book_id = borrow.book_id
JOIN student
ON borrow.stud_id = student.stud_id
where borrow.stud_id = '$stud_id'
ORDER BY 5 DESC ";
$query=oci_parse($link,$sql) or die ("error here!");
oci_execute($query);
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row->stud_id; ?></td>
<td><?php echo $row->book_id; ?></td>
<td><?php echo $row->book_title; ?></td>
<td><?php echo $row->book_bdate; ?></td>
<td><?php echo $row->due_date; ?></td>
<td><?php echo $row->return_date; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row->book_id; ?>'>Update</a></center>
</td>
<tr>
<?php
}
}
oci_close($link);
?>

Oracle returns field names as uppercase by default so you need to use uppercase indexes like so:
Here the solution where I got.
Btw thank you everyone for helping me.

Correct Your while loop You are fetching data as array:
oci_fetch_array($query, OCI_ASSOC) // return associated array
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row['stud_id']; ?></td>
<td><?php echo $row['book_id']; ?></td>
<td><?php echo $row['book_title']; ?></td>
<td><?php echo $row['book_bdate']; ?></td>
<td><?php echo $row['due_date']; ?></td>
<td><?php echo $row['return_date']; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row['book_id']; ?>'>Update</a></center>
</td>
<tr>
<?php
}

Related

Only single record getting when i fetch data from another page

i have created a php page for fetch all data from mysql and i have another page to show data on webpage....
i have php page its like page_1.php
<?php include_once'db_localconnection.php';
$query="SELECT * FROM `table 5` where base='Home Plans'";
$get_allplans=mysql_query($query) or die(mysql_error());
while($fetch=mysql_fetch_array($get_allplans))
{
$plans_code=$fetch['plan_code'];
$speed=$fetch['speed'];
$data=$fetch['data'];
$duration=$fetch['duration'];
$gb_pm=$fetch['gb_pm'];
$up_speed=$fetch['up_speed'];
$price=$fetch['price'];
$base=$fetch['base'];
}
?>
and i have another page for show data i have also include all needed pages
<td><?php echo $plans_code; ?></td>
<td><?php echo $speed; ?></td>
<td><?php echo $data; ?></td>
<td class="center"><?php echo $duration; ?></td>
<td class="center"><?php echo $gb_pm; ?></td>
<td><?php echo $up_speed; ?></td>
<td><?php echo $price; ?></td>
<td>get reacharge</td>
so i am getting only first record please help..
You storing the values from db to variables i.e $plans_code, etc. in WHILE statement you just overwriting the values each time you loop. Instead, store them into array and send to your second page and display them.
example:
$completeData = array();
while($row=mysql_fetch_array($get_allplans))
{
array_push($completeData, $row);
}
now fetch the array $completeData to your second page and then display it,
like:
<?php foreach($completeData as $row) { ?>
<tr>
<td><?php echo $row['plans_code']; ?></td>
<td><?php echo $row['speed']; ?></td>
.
.
.
</tr>
<?php } ?>

using get to retrieve value in php

I want to retrieve the information from a table to another using the GET function. but it's not working. please, what am i doing wrong?.
<td><?php echo $row['count(*)']; ?></td>
<td>N<?php echo number_format($row['sum(basic)'],2); ?></td>
<td>N<?php echo number_format($row['sum(hmo)'],2); ?></td>
<td>N<?php echo number_format($row['sum(dha)'],2); ?></td>
<td>N<?php echo number_format($row['sum(tax)'],2); ?></td>
<td>N<?php echo number_format($row['sum(netpay)'],2); ?></td>
<td><?php echo $row['year(date)'];?> </td>
</tr><?php }?>
the second page i want the the result to show in
$year = $_GET['year'];
$qry = "SELECT count(*), sum(basic), sum(hmo), sum(pension), sum(dha), sum(tax), sum(netpay), month(date) FROM salary WHERE year(date) ='$year' GROUP BY month(date)";
$run = mysql_query($qry) or die(mysql_error());
<?php while ($row = mysql_fetch_array($run)) {?>
<tr>
<td><?php echo $row['count(*)']; ?></td>
<td>N<?php echo number_format($row['sum(basic)'],2); ?></td>
<td>N<?php echo number_format($row['sum(hmo)'],2); ?></td>
<td>N<?php echo number_format($row['sum(dha)'],2); ?></td>
<td>N<?php echo number_format($row['sum(tax)'],2); ?></td>
<td>N<?php echo number_format($row['sum(netpay)'],2); ?></td>
<td><?php echo $row['month(date)'];?> </td>
</tr><?php }?>
This will work.
<td><?php echo $row['count(*)']; ?></td>
<td>N<?php echo number_format($row['sum(basic)'],2); ?></td>
<td>N<?php echo number_format($row['sum(hmo)'],2); ?></td>
<td>N<?php echo number_format($row['sum(dha)'],2); ?></td>
<td>N<?php echo number_format($row['sum(tax)'],2); ?></td>
<td>N<?php echo number_format($row['sum(netpay)'],2); ?></td>
<td>><?php echo $row['year(date)'];?> </td>
</tr><?php }?>
Reason: You were tried to pass value in month inside href. And tried to retrieve from get year parameter. Just change from month to year inside href. And you could able to retrieve year now. name parameter must to equal to get name parameter.
This is the URL in your link:
view_payroll_month.php?month=...
The query string parameter is called month. But you try to fetch a value called year:
$_GET['year']
Query string and/or form values are key/value pairs. The values are uniquely identified by the keys (names) with which they're associated. So you need to either:
Fetch the value by month instead of year, or
Pass the value year instead of month, or
Pass the value year in addition to month.
Something like this:
<a href="view_payroll_month.php?month=<?php echo $row['month(date)']; ?>&year=<?php echo $row['year(date)']; ?>">

display 2 tables on link by id

i have a table with id, name, address, sector, financiar, link
on the link i when i press it i want to show me 2 tables from the id of row selected, ex: id 1.
http://postimg.org/image/khelg1m0z/
and the result: http://s28.postimg.org/srvcwj065/Capture2.jpg
now it's a static page with search clause where by id 1, but i need an automatically link show by id on each row.
<?php
include "connect.php";
$sql = "select * from studenti where id='1'";
$query = mysql_query($sql) or die (mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Nume</td>
<td>Localitate</td>
<td>Judet</td>
<td>Sector Financiar</td>
<td>Link</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>
<?php } ?>
</table>
<?php
include "connect.php";
$sql1 = "select * from certificari where id='1' ";
$query = mysql_query($sql1) or die (mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Denumire certificare</td>
<td>Serie si numar certificare</td>
<td>Data certificarii</td>
<td>Valabilitate certificare</td>
<td>Sector Financiar</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['serie_numar']; ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['valabilitate']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<?php } ?>
</table>
You should not use the mysql_ functions since they are depricated and are very vulnerable to SQL injection attacks. Instead, I will use MySQLi in this answer, but you could also use PDO if you want to.
To display records for different ID's based on what the user selects you can pass an ID in the page URL. When you link to the page you add the desired ID to the address like this:
Link
The value of the variable id will now be available in page.php as $_GET['id'].
The actual PHP in your page would then look something like the code below. I have left out some of your HTML for brevity, but you can just add it int.
//Connect to the DB. Might want to put this in your connect.php and include it.
$db = new mysqli('localhost', 'user', 'pass', 'db');
//Prepare the statement. The ? will be your ID.
$statment = $db->prepare("SELECT * FROM studenti WHERE id = ?");
//Bind the parameters, so that the first (and only) question mark is turned into your ID.
//The 'i' means integer, if you store the id as a string your should use 's' instead.
$statement->bind_param('i', $_GET['id']);
//Execute the query.
$statement->execute();
//Get the results.
$result = $statement->get_result();
//Iterate over it to create the output.
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>
</tr>
<?
}
//Then do the same thing for the table certificari.
//Note that you only need to connect to the DB once.
This beginners guide to MySQLi is very helpful if you need some more guidance.

Any Echo value how to direct store in database

I have created a simple Business Management setup for office work. First I fetch all information from database. All is going well except the quantity input.
In this all payment doing calculate sum and do Echo. But I want that this echo value detect automatic store in database in another table.
so how to complete this?
This is the code of program.
<tbody>
<?php
$respectivestud = mysql_query("select * from client where client_id");
$i = 1;
while($r_client = mysql_fetch_array($respectivestud))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $r_client['name']; ?></td>
<td><?php echo $r_client['project_name']; ?></td>
<td><?php echo $r_client['cost']; ?></td>
<td><?php echo $r_client['payment_1']; ?></td>
<td><?php echo $r_client['payment_2']; ?></td>
<td><?php echo $r_client['payment_3']; ?></td>
<td><?php echo $r_client['payment_4']; ?></td>
<td><?php echo $r_client['payment_5']; ?></td>
<?php $total = $r_client['payment_1'] + $r_client['payment_2'] + $r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
<td> <?php echo $total ; ?></td>
<?php $remain = $r_client['cost'] - $total;?>
<td><?php echo $remain; ?></td>
**<td>**
Please make sure you write valid PHP code. You are not closing your while and you are not closing tags correctly. This should be correct.
<tbody>
<?php
$respectivestud = mysql_query("select * from client where client_id");
$i = 1;
while($r_client = mysql_fetch_array($respectivestud))
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $r_client['name']; ?></td>
<td><?php echo $r_client['project_name']; ?></td>
<td><?php echo $r_client['cost']; ?></td>
<td><?php echo $r_client['payment_1']; ?></td>
<td><?php echo $r_client['payment_2']; ?></td>
<td><?php echo $r_client['payment_3']; ?></td>
<td><?php echo $r_client['payment_4']; ?></td>
<td><?php echo $r_client['payment_5']; ?></td>
<?php $total = $r_client['payment_1'] + $r_client['payment_2'] + $r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
<td> <?php echo $total ; ?></td>
<?php $remain = $r_client['cost'] - $total;?>
<td><?php echo $remain; ?></td>
</tr>
<?php
}
?>
</tbody>
To save data you could use this:
$sql=mysqli_query($connection,"insert into `tablename`(`columnname`)values('".$total."')");
foirst off all replace mysql_* with mysqli_* or Pdo statement. as mysql_* is depricated now . here is the reference mysqli and pdo
store the remaining ammount in a variable. then use this variable in update query . like
<td><?php echo $remaining_amount=$remain; ?></td>
then update in db
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE MyGuests SET tablename='".$remaining_amount."' WHERE id=yprimarykey";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
<td><?php echo $r_client['payment_5']; ?></td>
<?php $total = $r_client['payment_1'] + $r_client['payment_2'] +$r_client['payment_3'] + $r_client['payment_4'] + $r_client['payment_5'];?>
<td> $sql=mysqli_query($connection,"insert into `tablename`(`columnname`)values('".$total."')");</td>
you can do like this whatever the value you want to insert...

Nothing on my page with an exception of the header after SQL query and table

I've been troubleshooting for a while now, and I really can't find any answer. Basically this is what it looks like:
When I type the name of a real user: Nothing posted on the page with an exception of the header
When I type a fake user: User has not been found (direct translation from Norwegian in the code)
<?php
include_once("moduler/head.php");
$user = $_GET['user'];
$query1 = $con->query("SELECT * FROM players WHERE name='$user'");
echo $con->error;
if ($query1->num_rows == 0) {
echo "<h1>Ingen spiller funnet med: $user.</h1>";
return;
}
while ($row == mysqli_fetch_array($query1)) {
$styrkeLvl = $row['styrke'];
$beskyttelseLvl = $row['beskyttelse'];
$bueskytingLvl = $row['bueskyting'];
$trehuggingLvl = $row['trehugging'];
$gruvedriftLvl = $row['gruvedrift'];
$fiskingLvl = $row['fisking'];
$kills = $row['kills'];
$deaths = $row['deaths'];
$rank = $row['rank'];
$money = $row['money'];
$donstatus = $row['donationstatus'];
$lastlogin = $row['lastlogin'];
$regtime = $row['registrationtime'];
$rankName = getRankString($rank);
?>
<h1><?php echo $user; ?></h1>
<table class=\"userView\">
<tbody>
<tr><td>Brukerstatus</td>
<td><?php echo $rankName; ?></td>
</tr>
<tr><td>Donasjon status</td>
<td>D<?php echo $donstatus; ?></td>
</tr>
<tr><td>Styrke level</td>
<td><?php echo $styrkeLvl; ?></td>
</tr>
<tr><td>Beskyttelse level</td>
<td><?php echo $beskyttelseLvl; ?></td>
</tr>
<tr><td>Bueskyting level</td>
<td><?php echo $bueskytingLvl; ?></td>
</tr>
<tr><td>Trehugging level</td>
<td><?php echo $trehuggingLvl; ?></td>
</tr>
<tr><td>Fisking level</td>
<td><?php echo $fiskingLvl; ?></td>
</tr>
<tr><td>Drap</td>
<td><?php echo $kills; ?></td>
</tr>
<tr><td>Dødsfall</td>
<td><?php echo $deaths; ?></td>
</tr>
<tr><td>Sist pålogget</td>
<td><?php echo $lastlogin; ?></td>
</tr>
<tr><td>Registreringsdato</td>
<td><?php echo $regtime; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
Any ideas? I've also tried running the entire thing in an echo with no result
Thanks
All your variables inside the while loop are only available inside the while loop. so you cannot echo them in the tables. so <td><?php echo $rankName; ?></td> will give you an undefined variable error. The same applies for the other variables you are trying to display.
To avoid this you should try to declare your variables globally, or put your table inside the loop.
Also the other issue is that as it is the code inside the loop will never execute because of the double equal (==). You should use double equal (==) for comparison and single equal (=) for assignment.
So you have to change while ($row == mysqli_fetch_array($query1)) to while ($row = mysqli_fetch_array($query1))

Categories