Title: sql ?
Last modified: August 20, 2016

---

# sql ?

 *  [dvalin](https://wordpress.org/support/users/dvalin/)
 * (@dvalin)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/sql-2/)
 * hay all.
 * Ok, sorta got done with what I could call my first real widget, it looks allows
   you to give it a list of blog id’s and it then lists the most recent post from
   them.
 * I currently have it listing 3 blogs and a small sample from each of their most
   recent posts.
 * The SQL calls im doing manually, because I dont know how to do it via wordpress,
   so my database password etc is all hardcoded which is fine if only I want to 
   use it, but I might want to let others use it.
 * So whats the wordpress method for calling sql queries and getting results. ??

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

 *  Thread Starter [dvalin](https://wordpress.org/support/users/dvalin/)
 * (@dvalin)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/sql-2/#post-2209678)
 *     ```
       $result = $wpdb->get_results($SQL);
               //$result = mysql_query($SQL)  or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());;
   
               while($row=mysql_fetch_assoc($result)){
       ```
   
 * thats what I am trying to do but its not working.. the comment out is how I was
   doing it before.
 *  [Damian Gostomski](https://wordpress.org/support/users/gostomski/)
 * (@gostomski)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/sql-2/#post-2209712)
 * You don’t even need to query the database directly for this…
 * WordPress has functions which let you switch which blog is currently being queried,
   so you could just switch to the differant ones, and then use query_posts as you
   would normally.
 * The functions you want are
 *     ```
       switch_to_blog($blog_id);
       restore_current_blog();
       ```
   
 *  Thread Starter [dvalin](https://wordpress.org/support/users/dvalin/)
 * (@dvalin)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/sql-2/#post-2209756)
 * never mind solved the problem
 *  Thread Starter [dvalin](https://wordpress.org/support/users/dvalin/)
 * (@dvalin)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/sql-2/#post-2209761)
 * Ah, I didnt know about those commands…
 * What I am doing now is this.
 *     ```
       $sql2="SELECT wp_".$row['blog_id']."_posts.post_date, wp_".$row['blog_id']."_posts.post_title, wp_".$row['blog_id']."_posts.post_content, wp_".$row['blog_id']."_options.option_value,wp_blogs.blog_id,wp_blogs.domain FROM wp_".$row['blog_id']."_posts, wp_".$row['blog_id']."_options,wp_blogs WHERE option_name = 'template' and wp_blogs.blog_id='".$row['blog_id']."' AND post_type='post' AND post_status='publish' ORDER BY post_date DESC LIMIT 0,1 ";
       ```
   
 * So with a single query I get out the info I need, that query is then part of 
   a loop that goes through all the blogs we have.

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

The topic ‘sql ?’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 4 replies
 * 2 participants
 * Last reply from: [dvalin](https://wordpress.org/support/users/dvalin/)
 * Last activity: [14 years, 10 months ago](https://wordpress.org/support/topic/sql-2/#post-2209761)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
