I'm trying to output the data from a specific table within my Oracle SQL Developer database using a PHP form. I had some experience with this concept in the past using MySQL but I'm struggling to understand the differences between integrating it using Oracle specific syntax now.
My code is ideally laid out but when I run the code I get an error regarding the OCI_FETCH_ARRAY parameters, which I'm unsure on how to solve.
My Code
<?php
include("../connection.php");
error_reporting(E_ALL);
$sql = 'SELECT * FROM ALGORITHM';
$stid = oci_parse($conn, $sql);
echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>Name</th> <th>Description<th> </tr>";
while($row= oci_fetch_array($stid, $sql))
{
echo "<tr>";
echo "<td>" . $row ['Algorithm_ID'] . "</td>";
echo "<td>" . $row ['Algorithm_Name'] . "</td>";
echo "<td>" . $row ['Algorithm_Role'] . "</td>";
echo "</tr>";
}
echo "</table>";
oci_free_statement($stid);
oci_close($conn);
?>
The Error I keep getting
Warning: oci_fetch_array() expects parameter 2 to be integer, string
given in
/studenthome.hallam.shu.ac.uk/STUDENTHOME8/1/b5040831/public_html/ADM/bin/php/entities/database.php
on line 12
I get that it's asking for an integer, but why? In the MySQL concept you simply outline the connection string it was never an issue.
Can anyone help ?
Calling oci_fetch_array() ( from http://php.net/manual/en/function.oci-fetch-array.php) should be called like
while($row= oci_fetch_array($stid))
The second parameter is the mode - i.e. OCI_ASSOC.
Update: when using oci_parse(), this doesn't actually execute the parsed statement, you need to do...
oci_execute($stid);
So your code would be something like...
$sql = 'SELECT * FROM ALGORITHM';
$stid = oci_parse($conn, $sql);
oci_execute($stid);
echo "<table border='1'>";
echo "<tr> <th>ID</th> <th>Name</th> <th>Description<th> </tr>";
while($row= oci_fetch_array($stid, OCI_ASSOC))
(Look at http://php.net/manual/en/function.oci-parse.php for code examples)
Related
I am passing over a factory operations system to a new support team and I am writing a guide for that.
It has a VERY simple DB section tucked inside and I just want very basic set of procedures for demonstration to the team who are very IT literate but do not have any DB or PHP experience.
I have finished most of the guide but having a bit of a problem with a simple Quantity update procedure.
Be clear - I have no problem doing it but I have searched and searched for a simple answer and also everything I do seems just far more complex than it needs be. Can anyone assist with simplicity !
As the base exampler I am using the well tried
<?php
$con=mysqli_connect('localhost', 'bbbbbb', 'bbbbb', 'bbbbbbl') or die(mysql_error());
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM orders_products");
echo "<table border='1'>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Product Quantity</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['products_id'] . "</td>";
echo "<td>" . $row['products_name'] . "</td>";
echo "<td>" . $row['products_quantity'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
which gives a simple table at the level I need
NOW all I want to demonstrate is how to update some or all of the Product Quantities in the list back to the MYSQL database. BUT AS SIMPLY AS POSSIBLE Without using individual "Edits" for each row. Apologies if this is too low level for you chaps !
NOTE: Edited to improve secrurity, but this does NOT negate the need for prepared statements to prevent other SQL injection attacks.
Wrap
<form method='POST' action='?'> around the table.
Replace
echo "<td>" . $row['products_quantity'] . "</td>";
With
$iProctId = $row['products_id'];
$iQuantity = $row['products_quantity'];
echo "<td>";
echo "<input type='text' name='product[{$iProductId}]' value='{$iQuantity}'/>";
echo "</td>";
In your script:
foreach( $_POST['product'] as $iProductId => $iQuantity ) {
mysqli_query( $con,"
UPDATE
orders_products
SET
products_quantity = ".(int)$iQuantity."
WHERE
products_id = ".(int)$iProductId."
");
}
Disclaimer
This script is simple, but not safe! To get it safe: mysqli_real_escape_string and mysqli_prepare
Enjoy :)
I have a simple PHP coding which fetches data from a specific table within my Oracle database. It works fine in outputting the data from the tables on the PHP form but one thing I've noticed is my Error_Reporting function calls up several identical errors indicating there are unidentified indexes present in the table.
My Code:
<?php
include("ConnectionCode.php");
error_reporting(E_ALL);
$sql = 'SELECT * FROM ROVER';
$stid = oci_parse($conn, $sql);
oci_execute($stid);
echo "<table border='1'>";
echo "<tr> <th>Rover ID</th> <th>Rover Name</th> <th>Launch_Date</th>
<th>Arrival_Date</th> <th>Manufacturer</th> </tr>";
while($row= oci_fetch_array($stid, OCI_ASSOC))
{
echo "<tr>";
echo "<td>" . $row ['ROVER_ID'] . "</td>";
echo "<td>" . $row ['ROVER_NAME'] . "</td>";
echo "<td>" . $row ['LAUNCH_DATE'] . "</td>";
echo "<td>" . $row ['ARRIVAL_DATE'] . "</td>";
echo "<td>" . $row ['MANUFACTURER'] . "</td>";
echo "</tr>";
}
echo "</table>";
oci_free_statement($stid);
oci_close($conn);
?>
I made the logical assumption that these indexes were due to the NULL values present within some fields of the table. But my question is mainly this, is there anything I can do in the code to fix this? Or is it not really an issue?
The manual mentions:
mode: An optional second parameter can be any combination of the
following constants:
[...]
OCI_RETURN_NULLS: Creates elements for NULL fields. The
element values will be a PHP NULL.
I'm not very familiar with the OCI interface, but the implication here seems to be that yes, NULL fields are omitted by default, but you can return them by explicitly setting that flag:
while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS))
This question already has answers here:
How can I use PDO to fetch a results array in PHP?
(2 answers)
Closed 2 years ago.
I have a php script that selects data via mysql_, however recently I have been reading that PDO is the way to go and that mysql_ is becoming depreciated. Now I am converting that script to PDO.
My question is though, I am not using $_POST to select. I just want to select the entire table with all of its data so I enter this query :
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $query->fetchall(); // or you can just $result = $query as hakre proposed!
so then like I did with my old depreciated mysql_ version of the script I used the echo to echo a table with the data in it.
echo
"<table border='2'>
<tr>
<th>ID</th>
<th>A Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Why</th>
<th>Comments</th>
<th>Signintime</th>
</tr>"
;
foreach($result as $row)
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" .$row['anum'] . " </td>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['why'] . "</td>";
echo "<td>" . $row['comments'] . "</td>";
echo "<td>" . $row['signintime'] . "</td>";
echo "<td> <input type=\"button\" value=\"Start Session\"onClick=\accept.php?id=" . $row['id'] . "&start=true></td>";
}
echo "</tr>";
echo "</table>";
now using this, I can not get a single output to my table.
My question is am I missing something from my select statements? Or am I not fetching any rows? Also I the connection settings set in another script called connect.php that is required by init.php (at the top of all of my pages)
Edit : 1
Edited the code so it now works, also adding a picture to show others how it should look! Hopefully some one can put this to some sort of use!
You are doing too much actually:
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $dbh->query($query);
The problematic line is:
$result = $dbh->query($query);
Check with http://php.net/pdo.query, the parameter is a string, actually the SQL string you already use above, not the result value of a PDO::prepare() call.
For your simple query you can just do:
$result = $dbh->query("SELECT * FROM students");
Or if you like to prepare:
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $query;
The later is some boilerplate if you want to insert variables into the query, that's why you prepare it.
The next problem is with the foreach line:
foreach($result as $row);
You are terminating the loop immediately because of the semicolon ; at the end. Remove that semicolon so that the following angle-bracketed code-block becomes the body of the foreach-loop.
Your code is wrong:
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $dbh->query($query);
After executing a prepared statement, you can just call fetchAll() on it:
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $query->fetchAll();
The rest of your code will work fine once you remove the semicolon after the foreach.
Basicaly having issues setting up a webpage which will taken in a student key entered by the user. This will then parse the student key to another file which will run it against a mysql backend to see what records this student already has. But can not get it working for the life of me please help I'm still a newb at this.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("support_log", $con);
$result= mysql_query("SELECT student.first_name, student.surname, student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY like '$_POST[stkey]'");
$result2 = mysql_query($result) or die("Error: " . mysql_error());
if(mysql_num_rows($result2) == 0){
echo("no records found");
} ELSE {
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Surname</th>
<th>Year Group</th>
<th>Student Key</th>
<th>Issue</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['First_Name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['year_group'] . "</td>";
echo "<td>" . $row['stkey'] . "</td>";
echo "<td>" . $row['issue'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
?>
After changing my where statement to:
WHERE student.STKEY like '$_POST[stkey]'");
I am no longer reciving errors from PHP but now recieving the error Query was empty which is part of my code to detect if there is no results. Though I have tested that query in phpmyadmin and it spits out results. From looking at the code does anyone have any solutions? I have also checked the parse by running an echo on the post command to ensure the data being entered was correct.
Edit: Got rid of the whole result2 check now throwing a:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\stkey_submit.php on line 24
Try $_POST['stkey'] instead of $_POST[stkey]
EDIT : if you use it in a query, it would be preferable to do :
$stkey = mysql_real_escape_string($_POST['stkey']);
$sql = "SELECT ....... like '$stkey'";
mysql_query($sql);
$result= mysql_query("SELECT student.first_name, student.surname, student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY like " . $_POST["stkey"]);
How about storing the value of stkey on a variable before including it on the query?
$stkey = $_POST['stkey'];
$result= mysql_query("SELECT student.first_name, student.surname,
student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY LIKE '%$stkey%'");
You might also want to use MySqli or PDO instead of the MySql database API. Take a look at this post from Nettuts: http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/
I am trying to display a table in php. I have established a valid connection. I get the error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Applications/XAMPP/xamppfiles/htdocs/project.php on line 17
The page's code:
<html>
<head>
<title>PHP Site Michael Mazur</title>
</head>
<body>
<?php
//connect to DB
$con=mysql_connect("localhost","mike","mike");
$db_found = mysql_select_db("my_guitar_shop2");
$result = mysql_query("SELECT firstName,lastName FROM customers");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['lastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
The rest of your while loop could look like this
while($row = mysql_fetch_array($result)){
print "<tr><td>".$row['Firstname']."</td><td>".$row['Lastname']."</td></tr>";
}
print "</table>";
Try putting these lines on a single line as i have done above.
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
like
echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th></tr>";
Other useful options here.
http://php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/control-structures.foreach.php
Given that part of the page's code is missing, this is only a guess. But it looks like part of your problem is that you've double-selected the database (a no-no).
Also, the while statement looks slightly suspect (no opening brace to verify context).
Additionally, if you are going to pass $con to the database selector, you should also pass it to the mysql_query calls (good practice for readability).