I have connected a oracle database with PHP. When I try to query a data in sqlplus it works. But when I try to query in PHP, it doesn't work(It doesn't show any error or nothing). My emp_id is number.
<p> $query = "select order_id from ordered_by where order_emp_id =".$emp_id."and order_done='N'";
$stid = oci_parse($conn, $query);
$r = oci_execute($stid, OCI_DEFAULT);
$value="";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
foreach ($row as $item)
{
$item!== null ? htmlentities($item) :'NULL';
$value =$item;
}
Put a space after =".$emp_id."
$query = "select order_id from ordered_by where order_emp_id =".$emp_id." and order_done='N'";
Related
How to get total number of row by using this function php mysql ?
i use this code for display data from mysql. It's work good,
Buy i want to know can i get total number of row by using this code ?
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
Try this mysql_num_rows ($result)
Use this line of code
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
$number_of_rows = mysql_num_rows($result); // this will return the number of rows found by the $result query.
echo $number_of_rows;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
I try to write a logIn.php. But the number of rows always returns 0. Thanks for your help!
$sql = "SELECT COUNT(*) as c FROM users WHERE '.password.' = '.$password.' AND '.name.' = '.$username.'";
$stid = oci_parse($dbh, $sql);
oci_execute($stid);
oci_fetch_all($stid, $result);
$row = $result['C'][0];
echo $row;
I am trying to connect to Oracle database from the following PHP script to populate a drop down list but the script doesn't work.
Can anyone see an issue? Many thanks!
$conn = oci_connect('username', 'password', 'host');
$stid = oci_parse($conn, 'select product_id, product_name from product order by product_id');
oci_execute($stid);
$query = "select product_id, product_name from product order by product_id";
$res = mysql_query($stid);
echo "<select name = 'Product'>";
while (($row = mysql_fetch_row($res)) != null)
{
echo "<option value = '{$row['product_id']}'";
if ($selected_product_id == $row['product_id'])
echo "selected = 'selected'";
echo ">{$row['product_name']}</option>";
}
echo "";
Why are you using mysql_* to interrogate an oracle database?? I think the right function to use is oci_execute
$res = mysql_query($stid);
The above line in your code is used to query a MySQL database,not Oracle.
I execute this query with php and odbc driver
$sql="DECLARE #Auftrag int;
DECLARE #date_now datetime = getdate();
EXEC #Auftrag=EHS.dbo.SP_ANZEIGE
#Tablet=1,
#Status=0,
#KuNr='K015538';
SELECT 'generatedID'=#Auftrag;";
$res = odbc_exec($db1_link, $sql) or die(odbc_errormsg()); // returns resource(13)
$firstRow = odbc_fetch_array($res); // dies error
If i do odbc_fetch_array the error "No tuples available at this result index" is thrown.
If I run the exact same query in Management Studio everything works fine. It shows me the computed generatedID. What is the difference?
greets Alex
Try to prefix the query with:
set nocount on
That prevents SQL Server from sending rowcount updates, which UNIX clients sometimes mistake for actual rowsets
I had this same problem. In my case I was executing like this
$sql = "SELECT * FROM table1";
$resultSet = odbc_exec($sqllink, $sql);
while ($data = odbc_fetch_array($resultSet)) {
$sql = "SELECT * FROM table2";
$resultSet2 = odbc_exec($sqllink, $sql);//failed here
while ($data2 = odbc_fetch_array($resultSet2)) {
//something here
}
}
and I changed like this and it worked
$sql = "SELECT * FROM table1";
$resultSet = odbc_exec($sqllink, $sql);
// Create an array and store the results
$queryResult = array();
while ($data = odbc_fetch_array($resultSet)) {
// push the required content into the array
$queryResult[$data['id']]['name'] = $data[name];
}
foreach($queryResult as $row) {
$sql = "SELECT * FROM table2";
$resultSet2 = odbc_exec($sqllink, $sql);
while ($data2 = odbc_fetch_array($resultSet2)) {
// something here
}
}
I'm using sqlsrv driver, and I'm trying to make a query like this:
$query = "select * from products";
$result = sqlsrv_query($conn,$query);
echo $result;
This returns me nothing. What is wrong with the query or PHP code?
This is how it would be done with a mysql connection. I haven't used sqlsrv driver, so it may not work as is but you get an idea.
Bruno posted a link with sqlsrv driver detailed code.
The real issue is that you can't just echo $result. It's an array and you have to do it row by row or write a function that echoes the complete $result. That way you can also filter rows or columns and format every field as you like.
$query = "select id, title, price from products";
$result = mysql_query($conn,$query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num)
{
$id=mysql_result($result,$i,"id");
$title=mysql_result($result,$i,"title");
$price=mysql_result($result,$i,"price");
echo "<b>id: $id</b> Product: $title , Price: $price<br>";
$i++;
}
I agree with that the real problem is that you can't echo a result - you have to iterate through the results. Just so you have an example with the sqlsrv extension, here's how I'd do it:
$query = "select id, title, price from products";
$result = sqlsrv_query($conn, $query);
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
echo "<b>id: $row['id']</b> Product: $row['product'], Price: $row['price'];
}
-Brian