PHP: Can't Get Oracle Table to Display on Page - php

This is probably a really obvious error. Anyway, some details:
I have an oracle database that I need to extract data from to populate a table on a PHP page. The table is called Flowers and has Name, Price and Stock columns.
The part of the PHP code I'm having trouble with is this:
$titlevalue = Trim($_REQUEST['search']);
$query = "SELECT * FROM FLOWERS WHERE NAME = '$titlevalue'";
$stmt = OCIParse($connect, $query);
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
OCIExecute($stmt);
The rest of my PHP works -perfectly- when using a different table on my database, which I did as a test. Just in case, this is the code that prints the query results (it's part of an HTML table, but you can ignore that):
while(OCIFetch($stmt)) {
echo "<tr valign=top bgcolor=#F7D4A3>";
$fg1 = OCIResult($stmt,"NAME");
echo "<td width=75>";
echo $fg1;
echo "</td>";
// Display values in column two.
$fg2 = OCIResult($stmt,"PRICE");
echo "<td width=75>";
echo ($fg2);
echo "</td>";
// Display values in column three
$fg3 = OCIResult($stmt, "STOCK");
echo "<td width=75>";
include($fg3);
echo "</td>";
echo "</tr>";
}
No matter what $titlevalue becomes, I just can't get results with this table. I have also tested it with a generic $query = "SELECT * FROM FLOWERS";, but that didn't produce anything either.
Could someone please lend a hand? :( It's been a very long night.

If I may, two things just to note:
1) you're using '$titlevalue', i think that will translate as ... $titlevalue in the query, and not its value
2) Its late ... have you checked that your table is called FLOWERS (case sensitivity)
I'd comment, but I dont have the rep to do so.
If this was helpful, please +1 or click the tickmark. I normally try and use the full format when doing sql queries
SELECT * FROM `table name` WHERE 'x'
... etc. where possible

Related

What causes a query to exclude first record

PHP and MySQL:What causes a query to exclude the first record in a table:
for example i have a script like this:
$query = "SELECT * FROM cars WHERE car_name = 'BMW'";
$results = mysql_query($query);
echo "<table border='1'>";
echo "<tr>";
echo "<th>Vehicle Name:<th>";
echo "</tr>";
while($row = mysql_fetch_array($result)){
$name = $row['car_name'];
echo "<tr>";
echo "<td>$name<td>";
echo "</tr>";
}
echo "</table>";
All rows are returned except the first one.Please help a brother out folks.
Not an answer, but too long for a comment:
Let's take a peak at your table cars.
What does
$qs = array(
array('total #rows', 'SELECT Count(*) FROM cars'),
array('#BMW', "SELECT Count(*) FROM cars WHERE car_name='BMW'"),
array('#LIKE BMW', "SELECT Count(*) FROM cars WHERE car_name LIKE '%BMW%'"),
array('#car_names', "SELECT Count(*) FROM (SELECT distinct car_name as foo FROM cars) as bar")
);
foreach( $qs as $query ) {
echo $query[0], "<br />\r\n";
$result = mysql_query($query[1]) or die(mysql_error());
while ( false!==($row=mysql_fetch_row($result)) ) {
echo ' ', $row[0], "\r\n";
}
}
print if placed in your script instead of your posted code?
The output should be something like
total #rows<br />
6
#BMW<br />
2
#LIKE BMW<br />
3
#car_names<br />
4
BTW: the mysql_* extension is deprecated,
see http://docs.php.net/manual/en/mysqlinfo.api.choosing.php
EDIT
If two or more columns of the result have the same field names, the last
column will take precedence. To access the other column(s) of the same name,
you must use the numeric index of the column or make an alias for the
column. For aliased columns, you cannot access the contents with the
original column name
You are using mysql_fetch_array and this is what it says in the documentation. I never use mysql* functions so I wouldn't have jumped to this type of conclusion quickly. Use mysql_fetch_assoc($results) and I'm 99% sure it will resolve your issue. The why this would be different is in the paragraph above from the documentation. I assume your first row is identical to at least 1 of the below rows. Which means it is very likely you're missing more than just the first one. May or may not be the case.
END EDIT
Add 4 things to your code.
echo "<tr>";
echo "<th>ID</th>"; // THIS
echo "<th>Vehicle Name:</th>"; // Add closing tags........
echo "</tr>";
echo mysql_num_rows($results); // THIS (compare this to your MYSQL output row count)
while($row = mysql_fetch_array($results)){ **THIS... you have $results set with query, but $result here*** make sure both are $results OR $result
$id = $row['YOUR_AI_ID']; // THIS
$name = $row['car_name'];
echo "<tr>";
echo "<td>$id</td>"; // THIS
echo "<td>$name</td>"; // Add closing tags.......
echo "</tr>";
}
Go go do now. Come back with results.

Assigning (download) links to my queried results

So I've been bashing my head on this problem for some time and i just cant find an example to get it working for my own piece of code. Now i am hoping that some of you got a solution for me, or at least point me in the right direction. So the problem is the following:
A person can upload certain files and information to a database. The uploading of the files, so the file name, is in the same database row as its name, location etc. So for example: a person has several required fields such as name, location and upload file which together form one row in the database. This works all fine and the files are being uploaded to a folder named: uploads/participant-database (entire name is: mytestsite.nl:2222/CMD_FILE_MANAGER/domains/mytestsite.nl/public_html/Recap/wp-content/uploads/participants-database).
However, the problem is, that the person also can search for the database data (and retrieve it) by using a checkbox search system. Based on the persons given checkbox options, certain data (that matches the selection) is being showed. The question however is: how can i assign the downlaod links to the right database values? Ill make it a bit clearer with some images below:
Picture with the displaying / retrieving of the database information
The code which retrieves the database information is as follows (i just took 1 filter to give the query idea because else it would be a to big piece of code):
if(!empty($_POST['columns_location']) && !empty($_POST['columns_theme'])) { // empty() checks if the value is set before checking if it's empty.
// Runs mysql_real_escape_string() on every value encountered.
$clean_criteria_location = array_map('mysql_real_escape_string', $_REQUEST['columns_location']);
// Convert the array into a string.
$criteria_location = implode("|",$clean_criteria_location);
// Runs mysql_real_escape_string() on every value encountered.
$clean_criteria_theme = array_map('mysql_real_escape_string', $_REQUEST['columns_theme']);
// Convert the array into a string.
$criteria_theme = implode("|",$clean_criteria_theme);
$tmp = $wpdb->get_results("
SELECT
name_of_living_lab, location_of_living_lab, type_of_living_lab, theme_of_living_lab, stage_of_living_lab, living_lab_document
FROM
wp_participants_database
WHERE
location_of_living_lab REGEXP ('$criteria_location') AND theme_of_living_lab REGEXP ('$criteria_theme')
ORDER BY
name_of_living_lab ASC
");
}
The code that displays the results is as follows:
echo "<table>
<tr>";
echo "<th>Name of Living Lab</th>";
echo "<th>Location of Living Lab</th>";
echo "<th>Type of Living Lab</th>";
echo "<th>Theme of Living Lab</th>";
echo "<th>Stage of Living Lab</th>";
echo "<th>Living Lab document</th>";
echo "</tr>";
if(count($tmp)>0){
for($i=0;$i<count($tmp);$i++){
echo "<tr>";
foreach($tmp[$i] as $key=>$value){
echo "<td>";
$b=unserialize($value);
if(is_array($b)){
array_filter($b);
$counttwo = 0;
foreach($b as $y){
if ($counttwo++ > 1) echo ", ";
echo $y;
}
}
else{
echo $value;
}
echo "</td>";
}
echo "</tr>";
}
}
echo '</table>';
Now the question is, how do i assign the right links to the queried results? So in the picture example you can see a document table (which is a result of the query) and this text in it should be clickable and downloadable with THAT stored document. So i guess the $tmp result value should be checked for only the database column: documents and then the link should be created which connects the database value to the right document. Though, i have absolutely no idea how to do this (even after quite some research).
I hope you guys can help me or can give me some pointers! Thank you in advance!
***UPDATE***
The new display code (which doesn't work since it outputs the text 2 times) with the added suggestions of #dHaRa uMaraniYa :
if(count($tmp)>0){
for($i=0;$i<count($tmp);$i++){
echo "<tr>";
foreach($tmp[$i] as $key=>$value){
echo "<td>";
if($key =='DOC'){
echo ''.$value.'';
}
$b=unserialize($value);
if(is_array($b)){
array_filter($b);
$counttwo = 0;
foreach($b as $y){
if ($counttwo++ > 1) echo ", ";
echo $y;
}
}
else{
echo $value;
}
echo "</td>";
}
echo "</tr>";
}
}
echo '</table>';
Select living_lab_document as DOC
$tmp = $wpdb->get_results("
SELECT
name_of_living_lab, location_of_living_lab, type_of_living_lab, theme_of_living_lab, stage_of_living_lab, living_lab_document as DOC
FROM
wp_participants_database
WHERE
location_of_living_lab REGEXP ('$criteria_location') AND theme_of_living_lab REGEXP ('$criteria_theme')
ORDER BY
name_of_living_lab ASC
");
and check that if
if($key =='DOC'){
echo ''.$value.'';
}
else
{
echo $value;
}

Beginner: php and mysql table issue

very new to php and mysql so all help is greatly appreciated. I have tried to search the forums but not entirely sure specifically what I need to be searching for. I have a form which ask users to select a product and make a comment.
I need the information for a particular product to show on my product page instead of all of the information. (for example, I want the reviews for iPads to show on the ipad page)
This is the code that send the data to the database:
<?php
session_start();
include('connection.php');
$name=$_POST['name'];
$product=$_POST['product'];
$star=$_POST['star'];
$comment=$_POST['comment'];
mysql_query("INSERT INTO tt_review(name, product, star, comment)VALUES('$name', '$product', '$star','$comment')");
header("location: https://scm-intranet.tees.ac.uk/users/l1071039/tablet-takeover/index.html");
mysql_close($con);
?>
This is the current code to fetch the data onto my page:
<?php
include('connection.php');
$result = mysql_query("SELECT * FROM tt_review");
echo "<table border='1'>
<tr>
</tr>";
while($row = mysql_fetch_array($result)) //This function is calling the results variable and displaying them within the rows below
{
echo "<tr>"; //this code tells the page to output the table rows that are defined above
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['date'] . "</td>"; //each row is then executed using the table data function
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['star'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
This is a screenshot of the table on my webpage (as I say, I need it to only show the ipad reviews.
To select only one kind of product, you should add a where clause on your sql query:
SELECT * FROM tt_review WHERE product = 'Apple iPad'
You can give like this
"SELECT * FROM tt_review WHERE Product_name ='ipad'"
It will display only the information related to Ipad
Still If you dont understand please give me the name of the columns you used in the table
Firstly, mysql_* functions have been depreciated. Rather, use either PDO or MySQLi.
Secondly, your code is very vulnerable to SQL injection.
Thirdly, fix your select statement to the following:
SELECT * FROM tt_review WHERE product = 'ipad'

How to fetch data from my database into <td id="myId"> using php?

I have created a table of empty cells as follows
$sql1 = mysqli_query($mysqli,"SELECT * FROM production WHERE date(productiondate)='".$today."' AND productionline=1");
echo "<tr>";
echo "<td>Row #1</td>";
for($i=8;$i<=20;$i++)
{
echo "<td id=".$i."></td>";
} echo "<td></td>";
Now I want to insert values into specific cells according to their ids, but unable to do so since the following function will insert them serially.
while($row=mysqli_fetch_array($sql1))
{
echo "<td>".$row['productionquantity']."</td>";
}
echo "</tr>";
Any help will be greatly appreciated. And please let me know if any other snippets of my code are required to get a better understanding of my motive.
Do you have any $sql2 variable?
Because your mysqli_fetch_array() gets an "$sql2 parameter.

PHP, MYSQL noob joining(?) tables specifics

I am new to PHP and am making a social network as practice and to apply what I have learned in the "real world". Anyhow, I have two tables in a MySQL database that i am trying to display on my site in the same html table that is being rendered through an php echo.
here are the tables
(table1)
note_system:
-id,
-username,
-note
(table2)
comments:
-id,
-cid (equals id from note_system),
-username,
-comment
so someone makes a post and it saves to the note_system table then someone comments on the post and it saves to the comment table with the id from the note_system table so a relation can be established.
So what I am trying to do is get the post comments to display with the relevant post. I have gathered that I need maybe a JOIN or UNION to make this happen but I am at a complete loss on how to do it. Been racking my brain and doing tons of google searches but I am not really getting anywhere. Everything I try gives me errors. The Notes display just fine and as intended but I can't for the life of me figure out how to get the comments to show up there too.
Here is my code (don't laugh at the noob-ness of my PHP, this is my 2nd PHP program ever and I obviously have much to learn, I would like to clean it up at some point but for now I just want it to be functional)
<?php
// Display Note_Wire
$con=mysqli_connect($host,$username,$password,$dbname);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//format and display the Note_Wire results with comments
$result = mysqli_query($con,"SELECT * FROM note_system");
while($row = mysqli_fetch_array($result))
{
echo "<center>";
echo "<table class='note_wire'>";
echo "<tr>";
echo "<td>" . $row['username'] . "</td>" ;
echo "</tr><tr>";
echo "<td><a href=''>vote up</a>" . " " . $row['rank'] . " " . "<a href=''>vote down</a></td>" ;
echo "</tr><tr>";
echo "<td> <a href='{$row['link']}' target='blank'>{$row['link']}</a>";
echo "</tr><tr>";
echo "<td>" . $row['note'] . "</td>" ;
echo "</tr> ";
//add comments attempt this is where I would like the comments to be displayed
echo '
<td><form action="add_comment.php" method="POST">
<input type="hidden" name="username" value="';
echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
echo '" />';
echo '<input type="hidden" name="cid" value="';
echo $row['id'];
echo '" />';
echo '<textarea name="comment">comment...</textarea></td></tr>
<tr><td><input type="submit" value="comment" />
</form></td></tr>
';
echo "</table>";
// break before next note-wire record renders
echo "<br />";
}
echo "</center>";
?>
I hope my chicken scratch programming makes sense. Thanks for your time and knowledge.
Really the comments are a different data set from the actual post you could just use a second query to get all of the comments related to the post. But table joins are very useful and you should learn them. In this case you would join the note_system and comments table on the shared ID (the foreign key).
So like so:
SELECT *
FROM note_system
LEFT JOIN comments ON comments.cid=note_system.id
This is a literal joining of the tables so your output will include all columsn from both tables as long as there is a match for the joining expression. If there isn't a CID column in the comments table that matches then the values for those columns will be NULL in your output. (If you wanted to only return rows where there is a match you could use an INNER join as opposed to the LEFT OUTER join I've used above.)
This is a good page explaining SQL table joins.
Heres the basics step for you if your a beginner in using php and mysql..
FIRST : SET UP CONFIGURATION FOR DATABASE USER,PASS,HOST,DBNAME
$conn = new mysqli('database_server','database_username','database_password','database_name');
SECOND : Create a Query(you may insert your query here)..
$result= $conn->query("SELECT SUM(Total_Steps) AS value_sum FROM users");
FINAL : SHOWN RECORDS USING MYSQL FUNCTIONS LIKE..
while($row = $result->fetch_assoc()){
echo $row['dabatase_columnname'];
echo $row['database_columnname'];
}
for your query try to use this one or either create a relative one.
SELECT 'enter column needed here with their specific database allias ex. TABLE1.ID'FROM NOTE_SYSTEM TABLE1 LEFT JOIN COMMENTS TABLE2 ON TABLE1.ID = TABLE2.CID;

Categories