wordpress search form sql query - mysql

I am trying to build a custom search form that will query my wordpress DB for the results.
The problems I seem to encounter is that the form does not send anything to function upon action just says 404 not found.
The function to query the database is located at functions/theme-search.php and I have declared function search_db in it.
Where am I going wrong?
Thanks.

View this part of code. This isn't bulletproof, but I am assuming the first WP_Query call is for the search
function my_posts_request_filter($input)
{
if ( is_search() && isset($_GET['s'])) {
global $wpdb;
// Make your sql code
remove_filter('posts_request','my_posts_request_filter');
}
return $input;
}

Related

How to display all database queries made by Wordpress with ajax?

I am using $wpdb->queries to show all running queries made by wordpress. This working fine when using simple php function but it's not showing same results when call same function with ajax. eg: if my simple php function showing results 15 but my ajax call function will show only 5 queries.
You need to add define('SAVEQUERIES', true) to your configuration file, you can then list all the queries made for the current page by adding the following to your theme.
You can try like this:
if (current_user_can('administrator')){
global $wpdb;
echo "<pre>";
print_r($wpdb->queries);
echo "</pre>";
}
See the documentation for more details: http://codex.wordpress.org/Editing_wp-config.php#Save_queries_for_analysis

delete file after sending to user

I try to send a file to the user with this function
Yii::$app->response->sendFile($tmp_filename, 'test.RData');
Now I want the file to be deleted after sending. I know that there's an event handler for send method in yii/web/Response called EVENT_AFTER_SEND
I tried to access this handler with the following code:
Event::on(\yii\web\Response::className(), \yii\web\Response::EVENT_AFTER_SEND, function ($event) {
unlink($event->response->filename);
});
But my problem is
a) I'm not sure if this is the right way
b) how to access the filename inside the event
Any help is appreciated!
I had the same problem this week. The documentation says that we can use the $data parameter to add any variable we want on this callback. Here is a example:
Yii::$app->response->sendFile('/path/of/my/temp/file')->on(\yii\web\Response::EVENT_AFTER_SEND, function($event) {
unlink($event->data);
}, '/path/of/my/temp/file');
If you will send the file immediately, you could
header('....');
echo file_get_contents(path/of/file)
unlink(path/of/file);
You could think to create a temporary file, so the operative system will delete it.

how can I see what script initiated a specific mysql query

I am experiencing some troubles with certain MySQL queries. In trying to fix it, I need to find out what files exactly create these queries. Is there a way to see this in the MySQL log files or through some other, simpler way?
The only thing I can see is the time, the user and the query itself, but not which script has initiated this query. This is what I am trying to find out.
I found this class that could help you on achieve that.
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);
}
}

Can't locate html location of form

I need to find where the form is nestled within the code so I can remove a field.
I found the if statements that do the function, but I can't locate where the form is pulled from. THe component I use to process payments doesn't have any fields to adjust, so I have to go to the code.
https://www.chosenpeople.com/main/donate/index.php?option=com_dtdonate&controller=authorizenet&task=authorizenet&Itemid=360
This is the function that pulls the form. . .
if($row->paymentmethod =='authorize.net' && $paymentfrequecy!="once" && $paymentfrequecy!=""){
if(isset($_POST['donations']) && isset($_POST['startdate'])){
$adminmsg.="<tr><td>".JText::_( 'DT_START_DATE').":</td><td> ".$_POST['startdate']."</td></tr>";
$adminmsg.="<tr><td>".JText::_( 'DT_NUMBER_DONATIONS').":</td><td> ".$_POST['donations']."</td></tr>";
Controllers will trigger execution of other views, models etc.
You controller is named something like
/components/com_dtdonate/controllers/authorizenet.php
You need to look at its code (the authorizenet() function) to know what it's doing.
It should be loading the form either from
/components/com_dtdonate/models/forms/authorizenet.xml
or something similar, or building it in code in a view (the view name should be apparent in the controller function you just looked at) i.e.
/components/com_dtdonate/views/someviewname/tmpl/default.php

Coldfuson CFScript "setSQL" method was not found

I've got a Coldfusion component, with a method in it called getColumnNames
This just queries a MySQL table, and returns the columnlist:
remote string function getColumnNames() {
qProcessCars = new Query();
qProcessCars.setDataSource('#APPLICATION.dsn#');
qProcessCars.setSQL('SELECT * FROM sand_cars WHERE 1 LIMIT 1');
qProcessCars = qProcessCars.Execute().getResult();
return qProcessCars.columnlist;
}
If I access this remotely in the browser, with page.cfc?method=getColumnNames, then I get the expected list of columns back.
However, if I try to access this from inside another method within the component, I get an error
remote string function otherFunction() {
...
sColumns = getColumnNames();
...
}
The error dump for the above code returns the message "The setSQL method was not found".
So can anyone help me find out why it works as a remote call, but not when called from another method inside the same component.
Problem may be caused some kind of race conditions. If you make few calls which interfere, at some point qProcessCars may already be query result, so invoking method is not possible.
I would try to make the qProcessCars variable local scoped (var qProcessCars = new Query();
) and/or try to use another variable name for query results.
Next possible step is to enclose the query building/executing code into named lock.
Ah I've answered my own question again. Sorry.
I've used the same name qProcessCars else where in the component, I hadn't put var in front of them.
I don't know WHY that was causing the problem, but it was. Maybe setSQL can only be called once per query object?