Codeigniter - query does not execute from a specific controller - php

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 !

Related

Codeigniter query doesn't run even though it runs on the server

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?

Include PHP File and execute it multiple times

Yeah I know this is probably not the best way to perform this. However I have a script which I would like to call multiple times with parameters and hand over two variables. The code I have so far looks like this:
$testfeld = array('User1'=>'400028',
'User2'=>'400027'
);
foreach($testfeld as $key=>$value) {
$_GET['cmd'] = 'modify';
include("testmodify.php");
}
If I run this code, I get an error message:
Fatal error: Cannot redeclare show() (previously declared in testmodify.php:14) in testmodify.php on line 39
If I perform this, it only modifys me the first entry of the array and then throws me the error message above.
Sadly, I have only read access to testmodify.php.... So is there a way to perform this include with all the content of the array?
Put your code in the function, include it once and call as much as you want.
You are receiving this error because the file testmodify.php contains the definition for "show". A second inclusion of the file caused by your loop attempts to define the object show again, which cannot be done.
Here is another SO question about someone trying to redefine a function.
Php and redifining
Edit: the other answer by MilanG is how you could go about fixing it.
You can do something like (in your testmodify.php script):
if (!function_exists("show")) {
function show($parameters) {
/* do your stuff here */
}
}
A better thing to do, in this case, would be to perform include_once() in your testmodify.php script, making it point to a script file where you define your show() function. My approach should also work for you.
Hope that helps :)

function to convert string to integer -- call to undefined function

I am creating a function that converts a users initials (STRING) to their userid (INT)
problem is when I call the function I get a call to undefined func error because the below declared function is no where to be found in the Source!
// connect to database -- this works, I checked it
function convertRadInitToRadID($radInits){
$sqlGetRadID="SELECT id FROM sched_roster WHERE radInitials == '".$radInits."'";
$resultGetRadID=mysql_query($sqlGetRadID);
$radID=mysql_result($resultGetRadID,0);
return $radID;
}
...I then create and array ($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter) of user initials, it works with no errors I tested it independently
$randKey=rand(0,(count($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter)-1));
$randRad=$radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter[$randKey];
$randAssignedRadID=convertRadInitToRadID($randRad); // call to undefined function error here
when I view source the function definition code (where I define the function) is nowhere to be seen in the source. Very strange. I tried placing it around different areas of the script, same error. The function definition is declared appropriately and is wrapped in .
Very strange. The function just doesn't appear. Quotations, syntax, semi-colons, etc are all spot on.
No syntax errors. Advice?
I Strongly agree with Answer #1.
In addition a usual problems occur in php if you are defining function after calling it. i.e. your calling code is before function defination then it will not run and will give an error of undefined function.
You can create a class then define this function in that class and on the time of calling you can call that function with help of $this->function(args)
I think this will resolve your problem in mean while i am trying to run your code on my machine, lets see what happen
May be your function is a method of some class. So, if it is, you should use it in another way:
MyClass::convertRadInitToRadID($radInits) // if static method
or like this
$class = new MyClass();
$class ->convertRadInitToRadID($radInits)
Trying to make sense of your question... Are you trying to call the function using JavaScript? If so, remember that JavaScript is run on the browser, and PHP is run on the server (and this is why when you "view source" you don't see the function anywhere). To send data back from JavaScript to PHP you should use AJAX.
I found the answer: I was using Jquery UI tabs.... there must be a conflict with tabs. When I run the code without tabs there is no issue.
Thanks for the '==' fix.. appreciate it. my bad
thanks for reminding me about the 80 char varname code limit

Codeigniter DB errors after stored procedure call

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()

Is there a way to Find out which PHP script has updated the MySQL database?

I can find out that certain query has been fired to MySQL database from the mysqlBinLog. But is there a way to find which PHP file has executed those queries ?
My problem is- I am not able to trace which php file in the Application is firing a particular update query.
I have searched in the whole application for that particular update query but I didn't find any match. It is generated programatically somewhere and get executed.
Since the code footprint is too big, it is practically not possible for me to go and put a log statement everywhere.
You can wrap a logger class around the database access library (PDO) and log the query together with a backtrace.
class LoggerPDO extends PDO
{
function query()
{
return $this->logger('query', func_get_args());
}
private function logger($method, $args)
{
// log query
debug_print_backtrace();
// push to parent
return call_user_func_array(array($this, 'parent::' . $method), $args);
}
}
You can add other methods that need logging too, such as prepare() or execute().
If you're using mysql_ functions, good luck :)
U can put logs from where update queries are fired and get these logs info

Categories