Store oracle row value on php variable - php

I have a table in Oracle with this columns: IMGPOSX, IMGPOSY and IDIMG. Each row of the table has a different X Y value positions and an ID to identify witch of this values correspond to an specified ID.
For example: IDIMG = image1 > IMGPOSX = 20 IMGPOSY = 50
With this value then I build a html map image and load the image with an specified ID and put the results of the IMGPOSX and IMGPOSY on the margin-top and margin-left css properties.
I have found several example of how to get the values of the first line of the row but i don't know how to get the another ones (the table has 12 rows)
With the next code I get the first row of each column (IMGPOSX, IMGPOSY and IDIMG) but i don't know how to get the rest of the rows of the table. If I put row1[1] the parser get an error.
<?php
$conn = oci_connect('TEST', 'TEST', 'ORCL');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, 'SELECT IMGPOSX, IMGPOSY, IDIMG FROM TESTTABLE ');
if(oci_execute($stid))
{
$row=oci_fetch_row($stid) ;
"<table border='2'>";
print"<tr><td><p>posx: </td><td>$row[0] </td></tr>";
print"<tr><td>posy: </td><td>$row[1] </td></tr>";
print"<tr><td>idimg: </td><td>$row[2] </td></tr>";
print"</table></br>";
}
oci_free_statement($stid);
oci_close($conn);
?>
Also I like to filter the results of the fetch by the idimg, for example, some code that says "showme only the IMGPOSX and IMGPOSY from the IDIMG='image2'.

A rough example could be appending a WHERE clause to your query:
$id = 'image2';
$stid = oci_parse($conn, "SELECT IMGPOSX, IMGPOSY, IDIMG FROM TESTTABLE WHERE IMDIMG = $id;");
or, with the same query, you can filter with IF:
if ($row[2] == $id) {
//show
}
The sentence before your code states that you could need more than one row, so if you are not filtering, you will recieve more, to get them you need a proper fetch. Your current one if fetch_row, maybe you need fetch_array in order to recieve array with all values.
I would suggest oci_fetch_assoc http://php.net/manual/en/function.oci-fetch-assoc.php so you can access the column via their names. Also the filtering will use a named column.
if ($row['IDIMG'] == $id) {
//show
}
Have in mind the following sentence of the function description:
This function is typically called in a loop until it returns FALSE
You'd need something like:
<?php
//all the code here
// ...
// ...
?>
<table border="2">
<?php while ($row = oci_fetch_assoc($stid)): ?>
<tr><td><p>posx:</p></td><td><?=$row['IMGPOSX'];?></td></tr>
<tr><td><p>posy:</p></td><td><?=$row['IMGPOSY'];?></td></tr>
<tr><td><p>idimg:</p></td><td><?=$row['IDIMG'];?></td></tr>
<?php endwhile; ?>
</table><br />
<?php
oci_free_statement($stid);
oci_close($conn);
?>

Related

Shows Table and Values Using Drop Down

In this application I have a drop down so you can select what table you want to view. Using Ajax I push the variable $tbl as an integer and the code below prints out the table that corresponds with that integer. Something is not working though and I need an extra pair of eyes to help debug.
if($tbl == 9){$table = "person";}
$conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
$query = mysqli_query($conn, "SELECT * FROM $table ") or die(mysqli_error($conn));
if($query){
echo '<table border="1"><thead><tr>';
$colnames = array();
while ($finfo = mysqli_fetch_field($query)) {
$name=$finfo->name;
array_push($colnames, $name);
echo "<th>".$name."</th>";
}
mysqli_free_result($query);
echo '</tr></thead><tbody>';
$count = 0;
echo $colnames[$count]; // this test prints the correct column name but it ends up being printed OUTSIDE the table for some reason
while ($row = mysqli_fetch_array($query, MYSQLI_BOTH)){
echo '<td>'.$row[$colnames[$count]].'</td>';
$count++;
}
echo "</tbody></table>";
}
The last while loop is suppose to echo out each field value but it isn't executing <tbody> is left blank.
You called mysqli_free_result($query) too early. So by the time you called mysqli_fetch_array, the sql result was already gone. Move mysqli_free_result after the last loop.
I'm not sure whether it's a typo or that the following is where the problem lies, but you're missing opening and closing <tr> tags around the column names after <tbody> and before </tbody>. This may help explain why stuff is being printed apparently "outside" the table.

Retrieve multiple checkbox and display other related information from database in a table

I got this list of checkboxes that print out from database. I am able to retrieve data from database if user makes multiple checkbox and display it in table, but i cannot retrieved other information from database and display it in the same table.
This is my code:
<table border='1'>
<tr>
<th>TITLE</th>
<th>PERCENTAGE RESULT</th>
</tr>
<?php
if(isset ($_POST["submit1"]))
{
$selectedcheckbox = $_POST["selectedcheck"];
$query = "SELECT * FROM compareresult where subject=$selectedcheckbox";
$sql_query = mysql_query($query) or die('Error 3 :'.mysql_error());
while($data = mysql_fetch_array($sql_query,MYSQL_ASSOC)){
$result=$data['result'];
}
foreach($selectedcheckbox as $title)
{
echo "<tr>";
echo "<td>".$title."</td>";
echo "<td>".$result."</td>";
}
echo "</tr>";
}
?>
I want to display result after user select multiple checkbox, so I wrote:
echo $result in table
so that the result can be displayed in table beside the selected title, but I am getting an error:
Array to string conversion in C:\xampp\htdocs\sam\c.php on line 31 Error 3 :Unknown column 'Array' in 'where clause'
I am not at the moment in the environment to test your problem, and I do this out of my head, but try something like the following.
if(isset($_POST['submit1'])){
$checkbox = isset($_POST['selectedcheck']) ? $_POST['selectedcheck'] : array();
foreach($checkbox as $title){
$query = "SELECT * FROM compareresult where subject='".$title."'";
$result=mysql_query($query); // <-- to avoid SQL injections, please change your method from mysql_query to mysqli_query (use the mysqli functions instead of mysql)
while($data = mysql_fetch_array($result)){
$result=$data['result'];
echo "<tr>";
echo "<td>".$title."</td>";
echo "<td>".$result."</td>";
echo "</tr>
}
}
}
It checks first if your checkboxes are checked and if they are they put it in an array.
Then it runs the foreach statement, where it sets the checkbox value as title. It runs there if the query if it the title is in the database, if so, spit it out through the while loop. Rinse and repeat until done.
edit
I see you put your open tablerow statement in the foreach but close it outside. So either you want it all printed on one line or is it an error? If its on one line, make sure you open the table row BEFORE the while loop and end it AFTER, else it's like how i spit it out.

PHP INSERT a variable number of records to mysql from a html form

I have a HTML form that retrieves a varying number product types that the user inputs stock figures. This data then needs to be INSERTED to a new table.
Here is the PHP query that populates the form.
require_once 'config.php';
$i = 1;
$sql = "SELECT * FROM dealer_product WHERE customer_code='$custcode' ORDER BY prod_code";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$prodcode = $row['prod_code'];
echo "<tr><td><input type='text' name='prod".($i++)."' value='" . $prodcode . "'/></td><td><input type='number' name='openstock".($i++)."'/></td><td><input type='number' name='sold".($i++)."'/></td></tr>";
}
mysql_close($con);
?>
I know how to INSERT a set number of multiple records, but how do I INSERT a varying number of records?
Thanks in advance. Sorry for my basic knowledge, I'm a network admin not PHP MYSQL.
Name your input fields as if they were arrays, e.g.:
<input name="prods[0]" />
You can then output a variable number of inputs in your HTML, even add more with JavaScript. PHP will convert the input to an array over which you can iterate:
<?php
foreach ($_POST['prods'] as $prod) {
/* Process $prod */
}
?>
I recommend that you go in the same way but using
while ($row = mysql_fetch_assoc($result))
And now you have not numbers as index that is more complicated way to see things, better see associative way that's the column name from your table in mysql.
And you're doing a lot of increments there doing $i++ a lot of times. I don't know if you're doing that intentionally but if not just increment $i once.
Also the number of row can be reached using this:
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
Look inside the manual
http://us1.php.net/mysql_fetch_assoc

display data from a certain letter

ok i got this page working well but what would do i have to do to display data from a certain letter?
here is the code i got at present
<html>
<head>
<title>MySQLi Read Records</title>
</head>
<body>
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = "select * from contacts";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
//this will link us to our add.php to create new record
echo "<div><a href='add.php'>Create New Record</a></div>";
if( $num_results > 0){ //it means there's already a database record
echo "<table border='1'>";//start table
//creating our table heading
echo "<tr>";
echo "<th>Firstname</th>";
echo "<th>Lastname</th>";
echo "<th>Username</th>";
echo "<th>Action</th>";
echo "</tr>";
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$name}</td>";
echo "<td>{$surname}</td>";
echo "<td>{$mobile}</td>";
echo "<td>";
//just preparing the edit link to edit the record
echo "<a href='edit.php?id={$id}'>Edit</a>";
echo " / ";
//just preparing the delete link to delete the record
echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
</body>
</html>
i am wanting to place multiple entries like the following to dislay under the right letter
<h1>A</h1>
echo "<td>{$name}</td>";
echo "<td>{$surname}</td>";
echo "<td>{$mobile}</td>";
<h1>b</h1>
echo "<td>{$name}</td>";
echo "<td>{$surname}</td>";
echo "<td>{$mobile}</td>";
ECT ECT
what i am trying to acchieve is to dispaly all the surnames that begin with a then b
i have found this bit of code on this page http://www.sitepoint.com/forums/showthread.php?303895-Display-Data-Beginning-with-a-Particular-Letter
but how would i make this work for me?
i am novice (extremly) so any help be fantastic i tried to read tutorials i find it better with hands on :)
Updated ***************************
this is working great but now it only lists one line this is my code
<html>
<head>
<title>MySQLi Read Records</title>
</head>
<body>
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = " SELECT name,
surname,
mobile,
UPPER (LEFT(surname, 1)) AS letter
FROM contacts
ORDER BY surname";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
//this will link us to our add.php to create new record
echo "<div><a href='add.php'>Create New Record</a></div>";
if( $num_results > 0){ //it means there's already a database record
echo "<table border='1'>";//start table
//creating our table heading
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
if (!isset($lastLetter) || $lastLetter != $row['letter'])
{
echo '<h1>', $row['letter'], '</h1>';
$lastLetter = $row['letter'];
echo "{$surname}";
}
}
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
</body>
</html>
Since you are learning, I will give you the idea of how you can archive what you want and the functions you can use.
As you have mentioned you want all records displayed on the same page with their own alphabet letter as the header.
There is a few ways of doing what you want, the most common is to return the data ORDERed BY the field you want on the order you want, in this case ASC.
This will list all your records in alphabetical order.
From there you can use the function LEFT to extract the first letter as another field.
Now here is where you will use some PHP, from the above you already have your records ordered and the first letter for each record.
You can use something like this inside your while:
if (!isset($lastLetter) || $lastLetter != $row['letter'])
{
echo '<h1>', $row['letter'], '</h1>';
$lastLetter = $row['letter'];
}
Basically the above will check if $lastLetter has been set/defined which is not for the first record so it will enter the if, print your first header and set $lastLetter to the first letter.
After the first record it will be matching the $lastLetter against the $row['letter'] and once they are not equal, it prints the header again and update the $lastLetter with $row['letter'] which is the current letter.
Your MySQL query would look like this:
SELECT *,
LEFT(firstname, 1) AS letter
FROM contacts
ORDER BY firstname
However it is always better to define all fields you need instead of catching all the fields in case you have more fields on the table in question:
SELECT firstname,
surname,
mobile,
LEFT(firstname, 1) AS letter
FROM contacts
ORDER BY firstname
NOTE in case your names are not normalize you can use the UPPER function to make the letter uppercase like this:
SELECT firstname,
surname,
mobile,
UPPER(LEFT(firstname, 1)) AS letter
FROM contacts
ORDER BY firstname
See more about the UPPER function here
I suggest you do the following steps.
Update your mysql query statement
$query = "select * from contacts order by name_field";
Name_field or whatever is the column name so that you get the result set sorted in dictionary order.
Keep $previous to keep track of what letter you added last time.
Use this function (refer startsWith() and endsWith() functions in PHP) at each row to find whether the name value starts with a different letter from $previous. If there is a change then update $previous and include "" tags with the letter as you desire.
function startsWith($haystack, $needle)
{
return $needle === "" || strpos($haystack, $needle) === 0;
}
First of all you should change your SQL query to:
SELECT * FROM contacts ORDER BY name ASC
this will sort all of the contacts from A to Z. Now, to able to determine an initial alphabet of a contact name use substr function...
$initial_letter = strtoupper(substr($name, 0, 1));

PHP MySQL omit result if contains phrase

I need some help with some PHP and MySQL code. At the moment I have a php file which displays the contents of a table in my database. I would like it to omit results that match "Not Booked". So the file only shows me slots which are booked. Here is my code:
<link href="../css/pagestyle.css" rel="stylesheet" type="text/css" />
<?php
include("../config.php");
include("functions.php");
if($logged["level"] == "HDJ" OR $logged["level"] == "SA") {
echo "<div class=\"head\">View Booked Slots</div>
<img src=\"../images/spacer.png\" width=\"5\" height=\"5\">
<div class=\"board\">Here you can view booked slots.</div>
<img src=\"../images/spacer.png\" width=\"5\" height=\"5\">
<table width=\"580px\" class=\"board\" border=\>";
$order = "SELECT * FROM timetable";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[username]</td>");
</tr>");
}
echo "
</table>
";
} else {
echo ("<div class=\"caution\">Access is denied.</div>");
}
?>
Modify the query not to pull them in the first place.
$order = "SELECT * FROM timetable WHERE <the column> <> 'Not Booked';
Replace <the column> with the correct column name in your table where Not Booked appears.
It is often not advisable to intermix database logic and display logic as you have done here. Instead, you ought to do the query before outputting your table, and store its results in an array. Then loop over the array to display your table.
$order = "SELECT * FROM timetable WHERE somecolumn <> 'Not Booked'";
$result = mysql_query($order);
// Error checking
if (!$result) {
// output error, take other action
}
else {
while ($row=mysql_fetch_array($result)){
// Append all results onto an array
$rowset[] = $row;
}
}
Later, loop over the array to output your values. Dont' forget to escape it for HTML output with htmlspecialchars().
foreach ($rowset as $row) {
echo "<tr><td>" . htmlspecialchars($row['username']) . "</td></tr>";
}
In your mySQL code:
SELECT * FROM timetable WHERE myvariable <> 'Not Booked'
Michael's answer will only pull out results that MATCH 'Not Booked' rather than omit them. Tweak the code to:
$order = "SELECT * FROM timetable WHERE <the column> <> 'Not Booked';
And you'll get all the booked ones (assuming there are only two states)

Categories