I have a database that has a few stored procedures in it that I would like to call via CodeIgniter. I have the following code in my Model class:
$sql = "CALL `stored_proc`(1)";
$query = $this->db->query($sql); //This call breaks the DB :(
$this->db->select('status');
$this->db->where('id', $id);
$query = $this->db->get('table');
print($query->num_rows()); //line 1116
When I run this code, I get the following error:
Fatal error: Call to a member function num_rows() on a non-object in C:\server\apache\htdocs\application\models\let_model.php on line 1116
If I remove the query line, the select works properly. Also, if I replace the call to a stored procedure with say a SELECT command, it also works properly.
Is there something obvious I'm missing for why I'm getting this error? If there isn't a good answer, is there a way to work around this problem?
Thanks for your time!
Edit: After delving a little deeper into the problem, it seems that this error will occur if my stored procedure contains a SELECT command. UPDATES seem to work properly. Perhaps this problem has something to do with how CodeIgniter deals with SELECT results?
Since this question appears first on google, i've managed to solve this by using :
$this->db->simple_query($query);
Instead of the regular query function.
The problem was that the SELECTs in the stored procedure were causing problems in CodeIgniter. By making sure that all of the SELECTs were directed (i.e. with the INTO clause), the stored procedure was able to run successfully.
For example,
-- This causes an error in CodeIgniter when inside a stored procedure.
SELECT GET_LOCK('lock1',0);
-- This does not.
DECLARE _lock_result INT;
SELECT GET_LOCK('lock1',0) INTO _lock_result;
I am still unaware of the underlying causes of why this causes an error, but this solution will suffice for my current work.
use only
$query->num_rows instead of $query->num_rows()
Related
I am attempting to call a DB2 function from PHP using zend_db to connect to DB2, and calling a SQL statement like so:
SELECT colA, colB, colC, mylib.testfunc(colA, colB, colC)
FROM otherlib.tableABC
WHERE colA = ... etc
(Just for reference, this is to call an RPG program that returns a specific value, so it is only returning one value). This works in an ACS Run SQL Scripts window, but when I try on Zend, I'm getting the following error:
Fatal error: Uncaught exception 'Zend_Db_Statement_Db2_Exception' with message 'TESTFUNC in MYLIB type *N not found. SQLCODE=-204'
I have double-checked to make sure my function does actually exist and it's in mylib. Doing a google search came up with nothing really helpful. Why would it be telling me that it doesn't exist? Is there something specific I have to configure when connecting to DB2 through Zend_DB?
In case it's important, I made the function like this:
CREATE OR REPLACE FUNCTION mylib.testfunc(iA DECIMAL(2,0), iB CHAR(4), iC DECIMAL(2,0))
RETURNS CHAR(10)
MODIFIES SQL DATA
LANGUAGE SQL
BEGIN
DECLARE oReturn CHAR(10) DEFAULT ' ';
-- Here I call a procedure I created to call the RPG
-- and format data...
RETURN oReturn;
END
Thanks in advance.
Is the userid (account-name) for connecting to Db2 the same between PHP and 'ACS Run SQL Scripts' ?
If not, verify that the account used for PHP has execute access to the function and whatever stored-procedures or external programs/routines the function invokes.
Additionally, when your SQL function CALLs the stored-procedure consider using a qualified stored-procedure name.
I am using Laravel 4.2 and am calling a Stored procedure from a database on the server. Locally it works fine, but on the server, using the same DB, it gives error. The call is as follows (I just want to make select):
$result = DB::select('CALL sp_special_prices("'.$codClient.'", "'.$codProduct.'", "'.$quantity.'", "'.$grup.'", "'.$FirmCode.'")');
When running a product listing on the server, it works except for two articles, giving the following error:
Caught exception: SQLSTATE[HY000]: General error: 2053 (SQL: CALL sp_special_prices("C000000", "445706049", "1", "146", "75");)
I already checked the php versions, and am using 5.6 on both sites. Could it be some special configuration? Is that the DB is the same, the place to call the SP, is that it is different.
If the procedure doesn't return anything, you need to use DB::statement instead of DB::select. If it does return something, you need to use DB::select
If the procedure has variable behaviour (may or may not return data depending on input), I suggest you change the procedure to return some data for all input combinations
I face this problem:
I have a function in a Model which runs a select query: I call it from the controller A (not real name). When I call the same method, but from the controller B, the PHP script stops when the query executes.
This function receives a number or an array as parameter. I've set the value of the parameter within the model to be sure I don't send wrong data from the controller B, and it keeps happening.
The query:
$arrConversas = $this->db->where_in('codigo_conversa', $codigo_conversa)
->group_by(['codigo_origem','codigo_destino',
'tipo_origem', 'tipo_destino'])
->get('conversa_mensagem')->result();
I think the code isn't wrong because it runs normally from controller A.
What am I doing wrong?
EDIT:
Thank you guys. The problem was a procedure that was running before this query, I tried to flush the connection and clean the cache and the problemas was there yet, then I use a $this->db->reconnect(); . Not the best solution but this action won't be used everyday.
I saw on the debugging the error: "Commands out of sync; you can't run this command now".
Thanks again!
I think you did not write the $this->load->database(); in your constructor in the model.
The problem was a procedure that was running before this query, then I used $this->db->reconnect(); and it worked !
I have a query made by codeigniter's query builder.
Up until recently, the query was working fine.
Now, it gives us a blank page but I've made no changes to it.
I turned error reporting on and it gave the generic query of it didn't return any results
Call to a member function result() on a non-object in /application/models/order_model.php on line 311
I dumped the query and tried to run on the server itself, it works.
Is there anything else I can try to debug it?
I am developing an agent-port application in CodeIgniter with PostgreSQL by using mutiple schemas from a single database. The setup I have designed is working fine.
So the problem I am facing right now is when after some interval it starts showing the following errors on different pages, like:
Fatal error: Call to a member function result() on a non-object in
D:\xampp\htdocs\mangonet\application\modules\settings\models\adminmodel.php
on line 30
Fatal error: Call to a member function num_rows() on a non-object in
D:\xampp\htdocs\mangonet\application\modules\users\models\adminmodel.php
on line 56
Fatal error: Call to a member function row() on a non-object in
D:\xampp\htdocs\mangonet\application\modules\users\models\adminmodel.php
on line 86
Let me give one example
I try to find out a particular problem only if it always remains there. But when I refresh my page or remove that row() from the function
function get_item_by_id($table, $id)
{
return $this->db->get_where($table, array('id' => $id))->row();
}
it returns a black result and when I add it back, the problem gone, and it starts working fine.
I know the above fatal errors you can see above solutions are available. But my problem is a bit complex.
This sounds like a problem with database connections going away. I would recommend looking at debugging this on the application side. In particular I would start logging the following:
Database connection status on all problematic calls. In other words check and see if the database connection has closed or failed.
If the database connection has not failed, then the next thing to check is what the search path is. You can execute the following query to test it:
SHOW search_path;
It is possible that if this is related to multiple schemas, something is messing with that.