How to use PDO::lastInsertId()? - php

$sql='INSERT INTO complaints(complaint_id) values(default)';
$db->query($sql,array());
//get newly added complaint_id
$complaint_id=$db->lastInsertId();
I used above code to get last insert ID from complaints table. But it gives an error like
Fatal error: Call to undefined method EMMACore\Utils\DBConnection::lastInsertId() in /h... Can anybody tell me what is wrong in my application.Thanks.

In most case, the PDO instance is a property of EMMACore\Utils\DBConnection, so check the source code and find it out.
Something like: $db->getDbh()->lastInsertId(); where getDbh (or something like that) returns the PDO instance.
Edit: After seeing your result of var_dump(), that is for sure.

class db extends PDO
you can refer to this link PHP PDO Wrapper Class for example.

Related

How to call functions in other PHP files?

I am trying to call a function from one file to another in PHP, however, I am getting the following error:
Fatal error: Call to undefined method SQLite3::print_model_query_form()
I have two files. The first - dbfunctions.php contains the method print_model_query_form().
The second file query_models contains the following code:
include_once("functions/dbfunctions.php");
$db = new SQLite3('D:\xampp\sim\htdocs\dis_sqlite_database\disSQL3.db');
print $db->print_model_query_form("query_models.php");
The function looks a little like this:
function print_model_query_form($action, $current_values = 0){
$db = new SQLite3('D:\xampp\sim\htdocs\dis_sqlite_database\disSQL3.db');
if($current_values){
// set to previous values.
}else{
// get POST values.
}
// Code to print query form.
}
Thanks in advance for any help.
Since you only hit fatal error on 3rd line, $db should be instantiated successfully. So, the issue should be the print_model_query_form method.
Referring to PHP:SQLite3 - Manual
, there is no such built in method called print_model_query_form.
[Edit 1]
Try to use require_once instead of include_once to make sure you have included dbfunctions.php successfully.
[Edit 2]
Check If you are using PHP's built in SQLite3 class (check your php.ini for extension=php_sqlite3.dll or extension=php_sqlite3.so).
If this is the case, check your dbfunctions.php for:-
class Something
new SQLite3
function print_model_query_form
If all the above exists then you should replace your 2nd line with,
$db = new Something(..);
Note: It would be better if you can show dbfunctions.php instead or letting us make assumptions based on guessing.

Fatal error: Call to a member function sql_in_set() on a non-object in

My question is what exactly is this error message telling me? What do I need to do specifically?
Fatal error: Call to a member function sql_in_set() on a non-object in /home/savas/x/inc/recenttopicsfeed.php on line 15
I have a php page pulling users and info from a phpbb3 forum. I decided to add a recent topics side bar on the same page and use the code from elsewhere so it's not mine and I don't fully understand it. I get this error message.
I tried looking and can't figure it out, but so I guess I want to understand fully what this means to better try and solve it.
Thank you.
This error messages is explaining itself by saying that sql_in_set is being called on non object.
If you are familiar with OOPS concepts then you must know that whenever we are going to call some member functions of class, we have to do it through class objects.
e.g: $db=new Database();
$db->member_function();
In this example we are calling member_function by object $db of Database class.
As you have mentioned that you are using it phpbb3 form and copying code from somewhere else.
I suppose you are doing it something like:
$sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE ' . $db->sql_in_set('user_id', $sql_in);
as explained here: https://wiki.phpbb.com/Dbal.sql_in_set
So i suppose you are using it somewhere where $db is not accessible/not created yet.
Try understanding the flow of phpbb3 and pass variables according to it.

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

Best way to build a database of php errors using mysql?

I'm building a newsletter CMS and I want to loog any errors to a database with information like timestamp, error, userID and function info. What's the best way to do this?
I'm thinking I should build a class that handles the errors and inputs them into a MYsql table using PDO.
pseudo code:
class sbmtErr
{
private $errStrng;
protected function sndErr($err, $usrData, $mthd){
$sndErrPDO = new myPDO;
$this->errStrng = "INSERT INTO errTbl (error, userID, method) VALUES ('".$err."', ".usrData['usrID'].", '".$mthd."')";
$sndErrPDO->sqlQry($errStrng);
}
}
My problem here is I don't know how to isolate the method that threw the error.
Is there a better way to do this?
Thanks.
Extend the exception class. CMSErrorException , when thrown (i.e. On construct) use reflection to find out things like function, line number, etc. how you find the user's id depends on how you keep it.
Then, regardless of what you do when you catch it, you call a method (again from the construct) which logs it into the database.
Careful not to throw these custom exceptions inside of the exception code, as it might cause an infinite loop in case of a database error.

Categories