PHP Oracle display result - php

i have a problem displaying the result of a SQL query (against Oracle DB). My web page code is below. I am using tnsnames and run the webserver and the page on the oracle db server itself. Running the query in sqlplus/sql developer works fine.
TNSNAMES:
testdb =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.101)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = testdb)
)
)
The page displays the 2 variables and afterwards directly the footer.
How can i display the returned data? Can somebody please help me?
Thanks
<head>
<title>Reports</title>
<link href="Site.css" rel="stylesheet">
</head>
<body>
<?php include("Header.php"); ?>
<div id="main">
<h1>Report1</h1>
<h2>Report2</h2>
<p>Report3</p>
<?php
if (isset($_GET['var1'])) {
$var_storenr = $_GET['var1'];
}
if (isset($_GET['var2'])) {
$var_storearea = $_GET['var2'];
}
echo "<p>Var 1: " . $var_storenr . "</p>";
echo "<p>Var 2: " . $var_storearea . "</p>";
$db_user='testusr';
$db_pass='testusr';
$db_name='testdb';
$conn = oci_connect($db_user, $db_pass, $db_name);
$query = 'select id_store, store_des from np_stores where id_store in ('.$var_storenr.') and id_region = '.$var_storearea.';';
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, $query);
oci_execute($stid);
echo "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
<?php include("Footer.php"); ?>
</div>
</body>

i have figured it out. It was a problem with the oracle connection, mainly PHP and x64 Oracle client.
installed x32 client and works fine.
Thanks,

Related

use a PHP variable as the source in an iframe to play a youtube video

I have spent the better part of three days on this problem. I've tried several solutions that I've found on this site but with little success. What I'm trying to do is use a PHP variable to play a youtube video in an iframe. I'm also trying to run two MySQL queries with the same input. Here is my code as it sits right now. At this moment when it runs I get the table that I'm wanting, though I still need to format it. But the iframe isn't even showing up. A previous solution I tried would pull up the iframe but inside would be an error where I was basically passing the sql query to the iframe.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
$trainingid = $_POST["trainingid"];
$sql = "SELECT * FROM purpletrainer.trainingcontent WHERE trainingid = '$trainingid';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Training ID</th><th>Title</th><th>Training URL</th><th>Training Quiz URL</th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["trainingid"] . "</td><td>" . $row["trainingtitle"] . "</td><td>" . $row["trainingurl"] . "</td><td>" . $row["trainingquizurl"] . "</td></tr>";
}
echo "</table>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$videosql = "SELECT trainingurl FROM purpletrainer.trainingcontent WHERE trainingid = '$trainingid';";
$videoresult = $conn->query($videosql);
if ($videoresult->num_rows > 0) {
while ($videourl = $videoresult->fetch_assoc()) {
}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<div class="col-md-6">
<iframe width="420" height="315" src= '<?php echo htmlspecialchars($videourl); ?>' frameborder="0" allowfullscreen></iframe>
</div>
It's common practice not to use the variable $videourl outside of the while loop. (Put your iframe within the while loop).
sidenote: fetch_assoc() will create an array. It should be $videourl['trainingurl'];
Here's an example:
<?php
if ($videoresult->num_rows > 0)
{
while($videourl = $videoresult->fetch_assoc()){
?>
<div class="col-md-6">
<iframe width="420" height="315" src= '<?= $videourl['trainingurl']; ?>' frameborder="0" allowfullscreen></iframe>
</div>
<?php
}
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
edit for clarity and future readers
Note that the <?= $variable ?> opening tag has only been properly supported since php 5.4+. When not using this version, maintain the usage of <?php echo $variable; ?>

Cannot get URL from Database to link

I am doing a project and would be eternally grateful for help in getting my URl's to link. I have tried looking around to no avail. I have a database (4columns). The last one (link1) should link to videos with the specified URL.When the table comes up the URL's are not clickable (is there a way to simplify this say "click me"?). Here is my code. I've also attached an image of the table. This is really busting my brains, thanks.
<?php
$con = mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM videos";
$result = mysqli_query($con, $sql);
echo "<table>";
echo "<tr>
<th>topic1</th>
<th>subject1</th>
<th>link1</th>
</tr>";
while( $row = mysqli_fetch_array( $result)) {
$topic1 = $row["topic1"];
$subject1 = $row["subject1"];
$link1 = $row["link1"];
echo "<tr>
<td>$topic1</td>
<td>$subject1</td>
<td>$link1</td>
</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Table output
Try this:
<?php
$sql = "SELECT * FROM `videos`";
$result = mysqli_query($con, $sql);
?>
<table>
<?php
while($row = mysqli_fetch_assoc( $result)) {
?>
<tr>
<td><?php echo $row['topic1'];?></td>
<td><?Php echo $row['subject1'];?></td>
<td><a href="<?php echo $row['link1']; ?>" target="_blank">Click me</td>
</tr>
<?php } ?>
<table>
Or you can also use do while loop:
do{
echo '<tr>';
echo '<td>'.$row['topic1'].'</td>';
echo '<td>'.$row['subject1'].'</td>';
echo '<td><a href="'.$row['link1'].'" target="_blank">Click me</td>';
echo '</tr>';
} while($row = mysqli_fetch_assoc( $result);
I added the target attribute to open the link in a new window.
I looked at your code and i found a couple errors.
change $con = mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test"); to $con = new mysqli("localhost", "feedb933_charles", "pass100", "feedb933_test");
Then change if (mysqli_connect_errno()) to if (mysqli_connect_error()) {
Then change
$sql = "SELECT * FROM videos";
to
$sql = "SELECT topic1, subject1, link1 FROM videos";
or if you want to select one row
$differentvalue = ""; // value to run
$sql = "SELECT topic1, subject1, link1 FROM videos WHERE difvalue = ?";
difvalue is the value different frm the rest so the php code knows what to grab.
Using stmt means no sql injection
Add:
$stmt = $con->stmt_init();
if (!$stmt->prepare($sql))
{
print "Failed to prepare statement\n";
}
else
{
}
Then in side if (something) { } else { IN HERE }
(if you have WHERE diffvalue) Add:
$stmt->bind_param("s", $differentvalue); // $stmt->bind_param("s", $differentvalue); if text, $stmt->bind_param("i", $differentvalue); if integer
Then
Add:
$stmt->execute();
$list = $stmt->fetchAll();
then outside the if (something) { code } else { code }
Add:
echo "<table><tr><th>topic1</th><th>subject1</th><th>link1</th></tr><tr>";
foreach ($list as $row => $new) {
$html = '<td>' . $new['topic1'] . '</td>';
$html .= '<td>' . $new['subject1'] . '</td>';
$html .= '<td>' . $new['link1'] . '</td>';
echo $html;
}
echo "</tr></table>";
If you are still having problems goto the links listed
http://php.net/manual/en/pdostatement.fetchall.php
http://php.net/manual/en/mysqli-stmt.get-result.php
http://php.net/manual/en/mysqli-result.fetch-all.php
Hope this helps
<?php
$con=mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM videos";
$result = mysqli_query($con, $sql);
echo "<table>";
echo "<tr>
<th>topic1</th>
<th>subject1</th>
<th>link1</th>
</tr>";
while( $row = mysqli_fetch_array( $result)) {
$topic1 = $row["topic1"];
$subject1 = $row["subject1"];
$link1 = $row["link1"];
echo "<tr>
<td>$topic1</td>
<td>$subject1</td>
<td>$link1</td>
*//this href will give u the link as u asked. //be sure you store proper url. In case of exact video name saved in column thn can make the url for your web output. //ex:<a href="http://example.com/'.$link.'.extension">*
</tr>";
}
echo "</table>";
mysqli_close($con);
?>

query was empty error and I can't work out why

I'm starting to build a very simple product display system, mainly to build my own skills, but also for use on my website for work.
List.php
<html>
<head>
<title>Retrieve data from the database</title>
<?php
$username='';
$password='';
$database='';
?>
</head>
<body>
<ul>
<?php
// Connect to database server
mysql_connect(localhost,$username,$password);
// Select database
#mysql_select_db($database) or die( 'Unable to select database');
// SQL query
$sql = "SELECT * FROM products WHERE category = 30";
// Execute the query (the recordset $rs contains the result)
$result = mysql_query($sql);
// Loop the recordset $rs
while($row = mysql_fetch_array($result)) {
// Name of the person
$strName = $row['make'];
// Create a link to person.php with the id-value in the URL
$strLink = "<a href = 'product.php?id = " . $row['ID'] . "'>" . $strName . "</a>";
// List link
echo "<li>" . $strLink . "</li>";
}
// Close the database connection
mysql_close();
?>
</ul>
</body>
</html>
Product.php
<html>
<head>
<title>Retrieve data from database</title>
</head>
<body>
<?php
$username="";
$password="";
$database="";
// Connect to database server
mysql_connect(localhost,$username,$password);
// Select database
#mysql_select_db($database) or die( "Unable to select database");
// Get data from the database depending on the value of the id in the URL
$sql = mysql_query('SELECT * FROM products WHERE ID=' . $_GET["ID"]);
$result = mysql_query($sql);
if(!$result)
die(mysql_error());
// Loop the recordset
while($row = mysql_fetch_array($result)) {
// Write the data of the product
echo $row['category'];
echo "<p>";
echo $row["make"];
echo "<p>";
echo $row["description"];
echo "<p>";
echo $row["picture"];
}
// Close the database connection
mysql_close();
?>
<p>Return to the list</p>
</body>
</html>
Can be seen messing up HERE
If someone can help get this working for me I'd be very grateful!
Try this way, but not fully sure about this stubborn issue. You can try by changing the case of all keys of the $_GET array to lowercase using the array_change_key_case()
$get = array_change_key_case($_GET);
$id = $get['id'];
$sql = mysql_query('SELECT * FROM products WHERE ID=' . $id);
NB: Make first sure SELECT * FROM products WHERE ID=your_row_id return results or not?
I turned out that the link that the page 'list.php' was generating was incorrectly formatted.
Changed this:
$strLink = "<a href = 'product.php?id = " . $row['ID'] . "'>" . $strName . "</a>";
To
$strLink = "<a href = 'product.php?id=".$row['ID']."'>" . $strName . "</a>";
And it's now working exactly as it should!

Data from oracle in utf-8 with php

i have a problem about getting data from oracle database. How to get data in utf-8? browser shows symbols with echo, but data from oracle has ? marks instead of letters
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"></meta>
</head>
<?php
$tns2 = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521)) (CONNECT_DATA = (SID = sidname)))";
if ($conn = oci_connect("user","pass", $tns2))
{
echo "YAY!";
//oci_close($conn);
}
else
{
die("Nesiseka");
}
$stid = oci_parse($conn, 'SELECT * rowname where rownum<=10');
oci_execute($stid);
oci_close($conn);
echo "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
echo"įęėšųį";
?>
Simple solution. Only add 'AL32UTF8' to line if ($conn = oci_connect(username,pass, $tns2 , 'AL32UTF8')) Everything works now.
Thank you for help
Use this to make database connection:
$conn = oci_connect(username,pass, $tns2 , 'AL32UTF8')
set php encoding to utf 8
mb_internal_encoding("UTF-8");
link for more
http://php.net/manual/en/function.mb-internal-encoding.php

Calculate columns from database then pull into table cell

EDIT: I have successfully been able to calculate the value I was trying to get, but instead of calculating that value for each row, it is just calculating it once and posting that value everywhere. How do I make it recalculate for each row using the code I have?
Picture: http://img515.imageshack.us/img515/9064/example2w.png
New Code:
<html>
<head>
<title>PHP-MySQL Project 4</title>
<div align="center">
<p>
PHP-MySQL Project 4
<br/>
By: Ryan Strouse
</p>
</div>
</head>
<body bgcolor="#99FFFF">
<?php
$DBName = "surveys";
$DBConnect = #mysqli_connect("localhost", "students", "password")
Or die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";
if (!$DBConnect)
{
echo "<p> The database server is not available.</p>";
}
else
{
echo "<p> Successfully connected to the database $DBName</p>";
}
mysqli_select_db($DBConnect, $DBName);
echo "<p>Database -'$DBName'- found</p>";
$SQLstring = "SELECT * FROM surveys WHERE surveyCode = 'GEI001'";
$QueryResult = #mysqli_query($DBConnect, $SQLstring);
echo $SQLstring;
$row = mysqli_fetch_assoc($QueryResult);
$count_surveys = $row['surveyResponses'];
echo "<p>Total Responses: $count_surveys</p>";
$SQLstring2 = "SELECT * FROM results WHERE surveyCode = 'GEI001'";
$QueryResult2 = #mysqli_query($DBConnect, $SQLstring2);
echo $SQLstring2;
echo "<br/>";
$Row = mysqli_fetch_assoc($QueryResult2);
$SQLstring3 = "SELECT * FROM surveys, results";
$QueryResult3 = #mysqli_query($DBConnect, $SQLstring3);
$fetchrow = mysqli_fetch_assoc($QueryResult3);
$result_amount = (($fetchrow['resultResponses'] / $fetchrow['surveyResponses']) * 100);
echo "<table>";
echo "<tr><th>Commercial</th> <th>Views</th> <th>Percentage</th></tr>";
do {
echo "<tr><td>{$Row['resultDescription']}</td>";
echo "<td>{$Row['resultResponses']}</td>";
echo "<td>$result_amount</td></tr>";
$Row = mysqli_fetch_assoc($QueryResult3);
} while ($Row);
echo "</table>";
?>
<center>
<h3>Return To Main Page</h3>
<h3>Return to Menu</h3>
</center>
</body>
<footer>
<div align="center">
© Copyright Ryan Strouse ©
</div>
</footer>
</html>
I have two database tables and I am successfully pulling in column data into a table. The third cell of the table I would like to calculate a percentage out of some of the columns from the database. I'm not sure how to code this... I've tried to come up with something in the SELECT statement from another thread I found with no luck.
Here is a picture of the query I'm trying to get to work: http://img696.imageshack.us/img696/3862/examplegw.png
<html>
<head>
<title>PHP-MySQL Project 4</title>
</head>
<body bgcolor="#99FFFF">
<?php
$DBName = "surveys";
$DBConnect = #mysqli_connect("localhost", "students", "password")
Or die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";
if (!$DBConnect)
{
echo "<p> The database server is not available.</p>";
}
else
{
echo "<p> Successfully connected to the database $DBName</p>";
}
mysqli_select_db($DBConnect, $DBName);
echo "<p>Database -'$DBName'- found</p>";
$SQLstring = "SELECT * FROM surveys WHERE surveyCode = 'GEI001'";
$QueryResult = #mysqli_query($DBConnect, $SQLstring);
echo $SQLstring;
$row = mysqli_fetch_assoc($QueryResult);
$count_surveys = $row['surveyResponses'];
echo "<p>Total Responses: $count_surveys</p>";
$SQLstring2 = "SELECT * FROM results WHERE surveyCode = 'GEI001'";
$QueryResult2 = #mysqli_query($DBConnect, $SQLstring2);
echo $SQLstring2;
echo "<br/>";
$Row = mysqli_fetch_assoc($QueryResult2);
//this is where I am trying to calculate the value and then below it display in table
//cell # 3
$SQLstring3 = "SELECT *,((resultResponses/surveyResponses)*100) AS AMOUNT FROM surveys, results";
$QueryResult3 = #mysqli_query($DBConnect, $SQLstring3);
do {
echo "<table>";
echo "<tr><th>Commercial</th> <th>Views</th> <th>Percentage</th></tr>";
echo "<tr><td>{$Row['resultDescription']}</td>";
echo "<td>{$Row['resultResponses']}</td>";
echo "<td>$QueryResult3</td></tr>";
$Row = mysqli_fetch_assoc($QueryResult);
} while ($Row);
echo "</table>";
?>
<center>
<h3>Return To Main Page</h3>
<h3>Return to Menu</h3>
</center>
</body>
<footer>
</footer>
</html>
IF I understand what you are asking, you're trying probably to calculate the percentage of a value that you can find in a MySQL query's results. If that is so, then I'd use the function mysql_num_rows to get the total of the records and then in a while and if I'd have a counter of how many times I meet that value.
Then you just do simple math, for example:
result = (100 * counter) / mysql_num_rows
and have a percentage. Then you just echo the result wherever you want! :)
I hope I have understood your question correctly!
$result = mysql_query("SELECT yourRow FROM yourTable");
$aArray = array();
$cont=0;
while($col = mysql_fetch_assoc($result){
$aArray[$cont] = $col['yourRow'];
cont++;
}
That should give you a workable array for you to make your math with it and then echo it. Cheers

Categories