I'm facing with problem executing Oracle Procedure from PhP. Actually, I am using Doctrine to execute it.
What is interesting is the fact of any other Queries can be executed/fetched, but procedures.
Below, you can find the codes I managed to use, this Select works wonderfully:
$connection = $this->getApplication()->getDataSourceManager()->getEntityConnection();
$stmt = $connection->prepare("SELECT SYSDATE FROM DUAL"); //or any other select works nice
$stmt->execute();
However, any procedure wont work, this is one of them:
$connection = $this->getApplication()->getDataSourceManager()->getEntityConnection();
$stmt = $connection->prepare("call prc_nutr_values('$cdfil', '$cdserice', '001', '0000000036', 'S', '$selectdt', '$selectdt')");
$stmt->execute();
The procedure above, doesn't surge any changes in DB. It doesn't his procedure
Error found!
The main problem was that PhP doesn't throw any error (I don't know why. However, I made the transaction in a different way, creating a clean new connection and it threw the error, as following:
$entitiesPath = realpath(__DIR__."/../src/");
$isDevMode = true;
$entity_metadata_config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array($entitiesPath), $isDevMode);
$connection_params = array(
'driver' => 'oci8',
'user' => 'user',
'password' => 'pwd',
'host' => 'IP',
'port' => 'port',
'dbname' => 'ORCL',
);
$entity_manager = \Doctrine\ORM\EntityManager::create($connection_params, $entity_metadata_config);
$conn=$entity_manager->getConnection();
$sql ="CALL FUNCTION_NAME('$param1', '$param2', '$param3', '$param4', '$param5', $param6, $param7)";
$outputMeta = $conn->exec($sql);
Connecting through this way, I could receive the error detailed:
it was $param6 and $param7 (non-formatted dates)
to solve this...
I just replace $param6 and $param7 with TO_DATE('$param6', 'DATE-FORMAT') and TO_DATE('$param7', 'DATE-FORMAT')
where 'DATE-FORMAT' is the used date format ('MM/DD/YYYY', or other used date formats)
Related
so i have a very weird issue that its been quite sometime now and am still figuring out why it is is. i have a function in sql server called fn_md5 that accepts 2 parameters and then return it as a varbinary.
but if the function is called via a the php script in my web, the hash is different when you directly call it from the ssms, and all of my ideas are out anymore. all i know is that the fn_md5 function when called directly in the ssms outputs the correct hash that i want it to be.
soo this is my fn_md5 function:
USE [master]
GO
/****** Object: UserDefinedFunction [dbo].[UFN_MD5_ENCODEVALUE] Script Date: 5/17/2022 4:41:16 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ЗФјцён : UFN_MD5_ENCODEVALUE()
-- і»їл : ЖЇБ¤№®АЪї°ъ АОµ¦Ѕєё¦ АМїлЗПї© MD5 °ЄА» »эјє
ALTER FUNCTION [dbo].[fn_md5]
(
#btInStr VARCHAR(10),
#btInStrIndex VARCHAR(10)
)
RETURNS VARBINARY(16)
AS
BEGIN
DECLARE #btOutVal VARBINARY(16)
EXEC master..XP_MD5_EncodeKeyVal #btInStr, #btInStrIndex, #btOutVal OUT
RETURN #btOutVal
END
and this is my php script:
$xodbc_connect = odbc_connect('Driver={ODBC Driver 17 for SQL Server};SERVER=WIN-6QBM0ALD4G1\SQLEXPRESS', 'sa', 'superpasszz');
$statement = "select [dbo].fn_md5('passpass', 'useruser')";
$exec = odbc_exec($xodbc_connect, $statement);
$result = odbc_result($exec, 1);
$password = $result;
//var_dump($password);
# insert data
$data = array(
'username' => $username,
'password' => $password,
'name' => $username,
'serial' => $this->_defaultAccountSerial,
'email' => $email
);
# query
$query = "INSERT INTO "._TBL_MI_." ("._CLMN_USERNM_.", "._CLMN_PASSWD_.", "._CLMN_MEMBNAME_.", "._CLMN_SNONUMBER_.", "._CLMN_EMAIL_.", "._CLMN_BLOCCODE_.", "._CLMN_CTLCODE_.") VALUES (:username, :password, :name, :serial, :email, 0, 0)";
now if i do this script in the ssms:
select [dbo].fn_md5('passpass', 'useruser');
it will give me this, which should be the correct hash:
0x0634DF7B99E2CF2344C3362F8CB76729
but if i use the registration process via my web it will give me this instead:
0xE5E892FA86C13BB1DF93630458E7DC31
the more weirder is that i cannot see what is wrongg and my head is going to blow. am still constantly learning both languages please bare with me and i hope you can help me out :<
I'm trying to select a row from a table using mysqli but all I can get is a bunch of null values and I don't really know why. The same query works using normal php mysql and if I try to perform the same query on phpMyAdmin using the parameter I pass goes through fine.
Here's the code:
$con = mysqli_connect('localhost', 'user', 'pass',"db");
if (mysqli_connect_errno()){
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$coupon = $_GET['coupon'];
$sql = mysqli_prepare($con, "SELECT * FROM coupon WHERE coupon=?");
$sql->bind_param('s', $coupon);
$sql->execute();
$sql->store_result();
echo $sql;
returns
"affected_rows":null,
"insert_id":null,
"num_rows":null,
"param_count":null,
"field_count":null,
"errno":null,
"error":null,
"error_list":null,
"sqlstate":null,
"id":null
I already tried to search for an answer either here and on google but I couldn't find anything close to my problem.
What am I doing wrong?
You must decide if you're using procedural or OOP approach. From your code, it seems you call the procedural version of mysqli extension and afterwards you try using objects. See the documentation examples, both object oriented and procedural and decide on a single one.
author's final solution (moved from question content):
As suggested by user #RiggsFolly I wasn't fetching the results at all, plus I was mixing procedural and OOP approaches as suggested by user #Alex . Here's the working code for future reference to anyone who will arrive here with a similar problem:
$coupon = $_GET['coupon'];
if ($sql = mysqli_prepare($con, "SELECT * FROM coupon WHERE coupon=?;")){
mysqli_stmt_bind_param($sql, 's', $coupon);
mysqli_stmt_execute($sql);
mysqli_stmt_bind_result($sql, $ID, $coupon, $discount, $uses);
mysqli_stmt_fetch($sql);
$data = array(
'ID' => $ID,
'coupon' => $coupon,
'discount' => $discount,
'uses' => $uses
);
echo json_encode($data);
}else{
echo json_encode(FALSE);
}
What am I doing wrong here?
$adapter = new Adapter(array(
'driver' => 'Pdo_Firebird',
'database' => 'localhost:c:/firebird/mydb.fdb',
'username' => 'SYSDBA',
'password' => 'mypass'
));
$sql = 'SELECT * USERS';
$statement = $adapter->createStatement($sql);
$result = $statement->execute();
if I check $result->count() I always get zero (0). However I know this query should produce results.
I get no errors.
ok, so it appears I am actually getting a result, even though $result->count = 0.
So I have to add the following lines after my code above;
$resultSet = new ResultSet;
$resultSet->initialize($result);
foreach ($resultSet as $row)
{
echo $row->LOGIN . '<BR>';
}
Feels a little long winded. Is this the best way to do it? I presume I should add some check to see if any results where returned. But I can't see the correct way to perform this check.
there. I'm migrating from mysql to PDO structure, and i had an issue when tryign a foreach statement, if i could get some help, would appreciate. the structure that doesn't work is:
foreach ($con -> query('SELECT MIN(LEAST(L1_RMS, L2_RMS, L3_RMS)) AS menor_valor FROM afunda_eleva') as $array_min_afund)
{
$intensidade_elevacao[] = $array_min_afund['menor_valor'];
}
where
$con is my variable to connect to the database. (working fine).
The error that i get when i run this is:
"Invalid argument supplied for foreach()"
The problem is that i've used this same structure some lines beyond this in the program and it worked. Does anyone know a possible reason for this to be happening? Thanks in advance!
EDIT
$result = ($con -> query('SELECT MIN(LEAST(L1_RMS, L2_RMS, L3_RMS)) AS menor_valor FROM afunda_eleva'));
while ($row = $result -> fetch_assoc())
{
$intensidade_elevacao[] = $row['menor_valor'];
}
Something you're going to want to do is get PDO to throw exceptions. They're much harder to miss than warnings and errors.
Here's the PDO constructor I use...
$con = new PDO('mysql:host=localhost;dbname=your_db_name;charset=utf8', 'username', 'password', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
Now, assuming your query actually works, it's only going to return one row with one column so try the following
$stmt = $con->query('SELECT MIN(LEAST(L1_RMS, L2_RMS, L3_RMS)) AS menor_valor FROM afunda_eleva');
$menor_valor = $stmt->fetchColumn();
I am going to use Zend framework to access data from an oracle database. Previously I have the class I built to interact with the database (outside of framework), those are all procedural and function calls in the database (not SELECT statements), I have to bind variables and then execute them. I am looking to use Zend_db component to access oci8 adapter. Anyone knows how to do that or can point me to a tutorial, that will be helpful.
Thanks
A quick Google search yielded this PDF and this tutorial from Oracle. The Oracle tutorial shows how to bind variables and execute stored procedures. Perhaps those have what you need.
$dbAdapterConfig = array(
'driver' => 'Oci8',
'connection_string' => '192.168.0.70/pep',
'username' => 'xx',
'password' => 'xx',
'character_set' => 'AL32UTF8',
'platform_options' => array('quote_identifiers' => false)
);
$adapter = new \Zend\Db\Adapter\Adapter($dbAdapterConfig);
$result = $adapter->query('SELECT COUNT(*) as CNT FROM B2B_INFO_SHOP', Adapter::QUERY_MODE_EXECUTE);
if ($result)
echo $result->current()->CNT, "\n";
$sql = new Sql($adapter);
$select = $sql->select()
->from('B2B_INFO_SHOP');
$select->where(array('SHOPID' => 123));
$selectString = $sql->getSqlStringForSqlObject($select);
echo $selectString, "\n";
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
if ($result)
echo $result->current()["SHOPNAME"];