How do I display database query statistics on Wordpress site? - mysql

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

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

Dynamically generating SQL data on new page with PHP

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"

sql query for the fetching back the drop-down list selected option

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

Load Record by ID for View CakePHP 3

I have what is probably a very basic question. I have a column in my computer record with the id of the customer who owns the computer. I am trying to show their name in a table next to the computers' and found a temporary solution. I am using this in my view (which probably isn't proper MVC) and also makes the page take forever to load (I am showing 100 computers per page).
<td>
<?php
foreach ($customers as $customer):
if ($computer->cust_id == $customer->id) {
echo $this->Html->link(
$customer->first.' '.$customer->last,
['controller'=>'Customers', 'action'=>'view', $customer->id]
);
}
endforeach;
?>
</td>
I also tried to do $this->Customers->get($computer->cust_id) after loading the model in the Controller but it says I need a Customer Helper. What is the CakePHP method of taking care of this issue?
Thanks!

Display a MYSQL field using CAKE

<div class="nhplft">
<div class="view_item_title"><i><?= $users["First_name"]["First_name"]; ?> in MANHATTAN has (an)</i> <?= $item["Item"]["item"]; ?><br /></div>
that line of code is pulling the item name from the ITEMS table and the ITEM field... what can i do to display the user first_name from USERS table in the same database on this page http://whitelabel.neighborrow.com/app/items/view/2
Most likely you pull data in the standard CakePHP manner.
So, if your controller pulls data from the Users model:
$this->set( 'users', $this->User->find('first', ..other conditions..) );
Your view will output the first_name column from the database by doing:
<?php echo $users['User']['first_name']; ?>
If that doesn't make sense, I'd recommend you brush up on Cake (this is Cake-101).
From your view code I think you need to link your models together.
" $users["First_name"]["First_name"]; "
Is your Users table really called 'First_name' ?