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
Related
I'm trying to write a PowerShell script which fetches an HTML page from a website and extracts some information from it.
My code looks like this:
$html = (invoke-webrequest -uri $address).parsedHTML;
$bodyHTML = $html.body.getElementsByClassName("news-item")[0].innerText;
The script fetches the website fine. The important part of the website looks like this:
...
<DIV class=news-item>
Important Information
...
The Problem:
I always get an error message: "cannot index into a null array".
The getElementsByClassName()-Function does not return anything.
If I list all div's and show the class names:
$html.body.getElementsByTagName("div") | select className
it lists all the class names including "news-item", which I am looking for.
Does anybody have an idea what the problem might be?
The problem seems to be the PowerShell version used. The PowerShell version running on the machine was 4.11.
With PowerShell 5.1 on a different machine the code worked fine.
As a workaround, because PowerShell coult not be updated and I was only looking for div-elements, I used the following code:
$bodyHTML = ($html.body.getElementsByTagName("div") | where { $_.className -eq "news-item" })[0].innerText;
I'm having trouble with this website I am creating.
Basically I'm trying to create news articles on a page that once you click on the article you'd be taken to a page that would load the content for that article. Is it possible to set it up in a way that I can load the content on a new page depending on the SQL primary key of the article selected?
I can load the articles on the page from the sql database no problem its just determining what data is loaded on a new page that is beating me.
Thanks in advance for any advice anyone can offer in this, even if its just pointing me in the right direction.
I have tried searching online first with no luck so far.
You can use $_GET to create a dynamic content using one page only
EXAMPLE:
article.php?news_id=1:
<?php
if(isset($_GET['news_id'])){
$newsID = $_GET['news_id'];
//query all the data you need using that $newsID
}
?>
you can pass the news-id as variable in url and get that variable using $_GET method,then execute the sql query.
<?php
$id =$_GET['id];
?>
mysql-"Select * from Table where id =$id"
I have drop down list in my website and the options are stored in database in a separate table and fetched back to drop down list. Now whenever I select any option from drop down list the value of option is stored in a main table. Now how do I fetch back the selected option from the database to website?
Could you provide some more detail? I assume that when the user loads the page you want the dropdown menu to display the same value it had the last time they logged in or something like that?
If such be the case, you need a few things. To start with your body tag needs an onload function that will get called when the page is loaded:
<body onload="dosomething()">
In your javascript dosomething() function you will make an ajax call to the back end which will probably be a php script.
This script will fetch the value from mysql and return it.
Your ajax code will have an onreadystatechange() function which will receive the response from the back end and do something like
document.getElementById('foo').value=whatevercamebackviaajax
Written off the top of my head, and somewhat vague, but I or somebody could probably do better with a bit more information.
You need to add javascript to your tags.
Now we can see the scenario where we get the option for one single user from the database so that it will be easy for understanding and you shall make your own code if you need to customize it.
PHP method:
Scenario: Keep Selected the login username is equal to the clientname in select tag.
<?php
$select="SELECT * FROM TABLE where id='".$_GET['uid']."'";
// here `uid` is the logged in user id
$q=mysqli_query($conn,$select) or die($select);
$row=mysqli_fetch_array($q);
?>
<select name="sclient" id="sclient" class="reginput"/>
<option value="">Select Client</option>
<?php
$sel="select * from user where type='client'";
$que=mysqli_query($conn,$sel) or die($sel);
while($res=mysqli_fetch_array($que))
{ ?>
<option value="<?php echo $res['login_name']; ?>"<?php if($row['clientname']==$res['login_name']) echo 'selected="selected"'; ?>><?php echo $res['login_name']; ?></option>
<?php } ?>
</select>
Parameters:
$conn - Connection Variable to DB. Db Connectivity using mysqli
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;
}
I've noticed that a few Wordpress blogs have query statistics present in their footer that simply state the number of queries and the total time required to process them for the particular page, reading something like:
23 queries. 0.448 seconds
I was wondering how this is accomplished. Is it through the use of a particular Wordpress plug-in or perhaps from using some particular php function in the page's code?
Try adding this to the bottom of the footer in your template:
<?php echo $wpdb->num_queries; ?> <?php _e('queries'); ?>. <?php timer_stop(1); ?> <?php _e('seconds'); ?>
To explain pix0r's code:
$wpdb->num_queries is the number of database queries.
_e is for localization: http://faq.wordpress.net/view.php?p=50
timer_stop() returns the amount of time taken to render the page: http://codex.wordpress.org/Function_Reference/timer_stop
wordpress has a easy solution now
There is a function called get_num_queries() and as specified in other answers timer_stop().
<?php echo get_num_queries(); _e(' queries'); ?> in <?php timer_stop(1); _e(' seconds'); ?></p>
get_num_queries() - returns the number of database queries during the WordPress execution and accepts no parameters