• Hi, I’m upgrading an old WP site from 3.7. It has a lot of SQL queries that don’t seem to load anymore. Is it the case that after 3.9 you have to connect to the DB via WPDB? And if so, could someone give me an idea how I could convert something like the following to work again?

    <?
              $sql = "SELECT value FROM wp_acf_values WHERE wp_acf_values.field_id = '105' AND wp_acf_values.id = '234'";
              $result = mysql_query($sql) or die('Content was not loaded. Please refresh your page.');
              while($post = mysql_fetch_array($result)) {
    
                $sqlTitle = "SELECT meta_value FROM wp_postmeta WHERE meta_id = '".$post['value']."'";
                $result1 = mysql_query($sqlTitle) or die('Content was not loaded. Please refresh your page.');
                while($post1 = mysql_fetch_array($result1)) {
                  $spotlightTitle = $post1['meta_value'];
                }
    
              }
              ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • It’s not the connection, but the type of database. The database is not necessarily mysql, so those direct calls won’t work. (It was never a good idea to access the database directly.)

    You can leave the query variable, although it should use $wpdb->prepare
    https://developer.www.ads-software.com/reference/classes/wpdb/prepare/
    and then instead of mysql_query or mysql_fetch it needs to be the corresponding method of $wpdb. It’s best just to use the wrapper functions that do what the code is trying to do like get_post_meta.

    Thread Starter wyclef

    (@wyclef)

    Like this?

    $sql = $wpdb->prepare( "SELECT value FROM wp_acf_values WHERE wp_acf_values.field_id = '105' AND wp_acf_values.id = '234'" );
              $result = $wpdb->query ( $sql ) or die('Content was not loaded. Please refresh your page.');
              while($post = mysql_fetch_array($result)) {
    
                $sqlTitle = $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE meta_id = %s", $post['value'] );
                $result1 = $wpdb->query ( $sqlTitle ) or die('Content was not loaded. Please refresh your page.');
                while($post1 = mysql_fetch_array($result1)) {
                  $spotlightTitle = $post1['meta_value'];
                }
    
              }
    Thread Starter wyclef

    (@wyclef)

    I’m confused how mysql_fetch_array is ported over to WPDB. Seems like it uses get_results? I realize this is a very outdated way to go about this and fully intend to modernize this but in keeping a long story short it would be very helpful to be able to sort this out within this old jumbled paradigm if possible. Is this at least on the right track or am I way off?

    <?
    
    global $wpdb;
    
    $sql = $wpdb->prepare( "SELECT value FROM wp_acf_values WHERE wp_acf_values.field_id = '105' AND wp_acf_values.id = '234'" );
    $result = $wpdb->query ( $sql ) or die('Content was not loaded.');
    
    while($post = mysql_fetch_array($result)) {
    
    $sqlTitle = $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE meta_id = %s", $post['value'] );
    $result1 = $wpdb->query ( $sqlTitle ) or die('Content was not loaded.');
         while($post1 = mysql_fetch_array($result1)) {
            $spotlightTitle = $post1['meta_value'];
         }
    }
    ?>

    Wyclef, WP code does not use the mysql functions at all, if your code has a function that begins with mysql_ or mysqli_ then you’ve used wpdb incorrectly. WPDB uses WPDB and only WPDB, and all the return values are arrays, objects, or primitives.

    If you look at the docs you’ll see that none of the wpdb methods return a mysql results object, or any mysql objects at all, and none of the examples use mysql functions.

    e.g.

    $fivesdrafts = $wpdb->get_results( 
    	"
    	SELECT ID, post_title 
    	FROM $wpdb->posts
    	WHERE post_status = 'draft' 
    		AND post_author = 5
    	"
    );
    
    foreach ( $fivesdrafts as $fivesdraft ) 
    {
    	echo $fivesdraft->post_title;
    }
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Upgrading Old Site / Help with SQL to WPDB after 3.9?’ is closed to new replies.