Title: Retrieving data from database
Last modified: August 30, 2016

---

# Retrieving data from database

 *  Resolved [jeegrobot](https://wordpress.org/support/users/jeegrobot/)
 * (@jeegrobot)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/retrieving-data-from-database-2/)
 * Hi everyone,
 * I’m trying to write a plugin that should retrieve data from the database and 
   display them, but for some reason it’s not working. This is my code
 *     ```
       global $wpdb;
       	$domande = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."tstile ORDER BY Posizione_Gruppo ASC, Posizione_Domanda ASC;");
   
       	$arrayDomande = array();
       	foreach($domande as $domanda) {
       		if (!isset($arrayDomande[$domanda->Posizione_Gruppo])) {
       			$arrayDomande[$domanda->Posizione_Gruppo] = array('gruppo' => $domanda->Gruppo, 'domande' => array());
       		}
       		$arrayDomande[$domanda->Posizione_Gruppo]['domande'][$domanda->Posizione_Domanda] = array(	'ID' => $domanda->ID,
       																									'domanda_sx' => $domanda->Domanda_Sx,
       																									'domanda_dx' => $domanda->Domanda_Dx,
       																									'valore_sx' => $domanda->Valore_Sx,
       																									'valore_dx' => $domanda->Valore_Dx);
       	}
   
       ?>
       <form name="frm_stile" id="id_frm_stile" method="post">
       <?php wp_nonce_field('nonce_save_test_stile','nonce_stile'); ?>
       <input type="hidden" name="action" value="save_t_stile" />
          	<table class="table_test ab">
       		<thead class="table_header">
       			<tr>
       				<th class="h_risposta"></th>
       				<th class="h_domanda" width="45%">Opzione</th>
       				<th class="h_text">#</th>
       				<th class="h_domanda domanda2" width="45%">Opzione</th>
       				<th class="h_risposta"></th>
       			</tr>
       		</thead>
       <?php
       	$ix = 1;
       	foreach($arrayDomande as $posizioneGruppo => $infoGruppo) {
       ?>
       		<tbody>
       			<tr>
       				<th colspan="5" class="gruppo"><?php echo sprintf("Gruppo %s", ($ix)) ?></th>
       			</tr>
       <?php
       		$iy = 1;
       		foreach($infoGruppo['domande'] as $opzione) {
       ?>
       			<tr class="<?php echo($iy%2==0?'odd':'even'); ?>">
       				<td class="input" width="1">
       					<input type="hidden" name="ixd[<?php echo sprintf($opzione['ID']);?>]" value="<?php echo sprintf("%s,%s", $iy, $ix); ?>" />
       					<input type="radio" name="r[<?php echo sprintf($opzione['ID']); ?>]" id="id_domanda_sx_<?php echo sprintf($opzione['ID']); ?>" value="sx"/>
       				</td>
       				<td class="domanda"><label for="id_domanda_sx_<?php echo sprintf($opzione['ID']); ?>"><?php echo sprintf($opzione['domanda_sx'])?></label></td>
       				<td class="text numero-domanda"><?php echo sprintf($iy); ?></td>
       				<td class="domanda domanda2"><label for="id_domanda_dx_<?php echo sprintf($opzione['ID']); ?>"><?php echo sprintf($opzione['domanda_dx'])?></label></td>
       				<td class="input"  width="1"><input type="radio" name="r[<?php echo sprintf($opzione['ID']); ?>]" id="id_domanda_dx_<?php echo sprintf($opzione['ID']); ?>" value="dx"/></td>
       			</tr>
       <?php
       			$iy++;
       		}
       ?>
       		</tbody>
       <?php
       		$ix++;
       	}
       ?>
       		<tfoot>
       			<tr>
       				<td colspan="5"><button id="btnSave">Salva e visualizza risultato</button></td>
       			</tr>
       		</tfoot>
       	</table>
       </form>
       ```
   
 * Any help? Thanks in advance.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [kjodle](https://wordpress.org/support/users/kjodle/)
 * (@kjodle)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/retrieving-data-from-database-2/#post-6573848)
 * You might want to be more clear about “it’s not working”. Is it giving an error
   of any sort?
 *  Thread Starter [jeegrobot](https://wordpress.org/support/users/jeegrobot/)
 * (@jeegrobot)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/retrieving-data-from-database-2/#post-6573863)
 * No. Simply the data don’t appear.
 *  [kjodle](https://wordpress.org/support/users/kjodle/)
 * (@kjodle)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/retrieving-data-from-database-2/#post-6573889)
 * Have you tried doing a var_dump on $domande to see if it’s pulling the data from
   the database. If not, there’s either an issue with your search query or an issue
   with your database.
 *  Thread Starter [jeegrobot](https://wordpress.org/support/users/jeegrobot/)
 * (@jeegrobot)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/retrieving-data-from-database-2/#post-6573975)
 * Hi kjodle,
 * fist of all let me thank you for your help. I’ve discovered the problem, but 
   I’m unable to find a solution. The problem is that WordPress is installed in 
   a subfolder of another WordPress installation. wpdb is reading the root website
   database, instead of the subfolder installation database. How can I tell him 
   to read the right db?
 *  Thread Starter [jeegrobot](https://wordpress.org/support/users/jeegrobot/)
 * (@jeegrobot)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/retrieving-data-from-database-2/#post-6573977)
 * Ok, I found the solution. Just for your information, there is a call to wp-blog-
   header.php that goes to the root installation, by changing to the actual installation
   everything works.
 * Thanks for your time.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/retrieving-data-from-database-2/#post-6574035)
 * That’s great you found the solution!
 * To anyone in the future who lands here from search results, the OP’s experience
   is one of the reasons including or requiring wp-blog-header.php or wp-load.php
   is considered poor practice — it doesn’t transfer well to other installations.
 * There’s only a few ways to do this that transfers properly, one of many explanations
   is in [http://glennmessersmith.com/pages/wpenviro.html](http://glennmessersmith.com/pages/wpenviro.html)
 * jeegrobot – you don’t necessarily need to do anything different as long as your
   code is just used on your own site. If you wish to distribute it then you should
   review the linked article.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Retrieving data from database’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 6 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [10 years, 7 months ago](https://wordpress.org/support/topic/retrieving-data-from-database-2/#post-6574035)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
