admin管理员组

文章数量:1404577

i need connect another database in same host and return product value(woocomerce). The return is null


add_shortcode('product_project_test', 'product_project_test');
function product_project_test() {   
    $mydbTest = new wpdb('user','password','dbname','localhost');

    $result= $mydbTest->get_results("SELECT `meta_value` FROM `wp_postmeta` WHERE 'post_id' = 5338 AND 'meta_key' = '_sale_price'");
    $price = $result[0];
    $price = number_format($price, 2, '.', ',');

    return '$ '.$price;

}

SQL Consult

SELECT `meta_value` FROM `wp_postmeta` WHERE `POST_ID` = 5338 AND `meta_key` = '_sale_price'

Return

Update - my Solution works


add_shortcode('product_project_test', 'product_project_test');
function product_project_test() {   
    $mydbTest = new wpdb($userdb,$mypass,$mydb,'localhost');
    $query = "SELECT * FROM wp_postmeta WHERE post_id=608 AND meta_key IN('_sale_price', '_regular_price')";

    $results = $mydbTest->get_results($query, ARRAY_A);
        foreach($results as $result){
        if($result['meta_key'] == '_sale_price'){
            $price= $result['meta_value'];
            $price= number_format($price, 2, '.', ',');
        } else if($result['meta_key'] == '_sale_price'){
            $old_price= $result['meta_value'];
            $old_price= number_format($old_price, 2, '.', ',');
        }
    };

    return '$ '.$price;

}

i need connect another database in same host and return product value(woocomerce). The return is null


add_shortcode('product_project_test', 'product_project_test');
function product_project_test() {   
    $mydbTest = new wpdb('user','password','dbname','localhost');

    $result= $mydbTest->get_results("SELECT `meta_value` FROM `wp_postmeta` WHERE 'post_id' = 5338 AND 'meta_key' = '_sale_price'");
    $price = $result[0];
    $price = number_format($price, 2, '.', ',');

    return '$ '.$price;

}

SQL Consult

SELECT `meta_value` FROM `wp_postmeta` WHERE `POST_ID` = 5338 AND `meta_key` = '_sale_price'

Return

Update - my Solution works


add_shortcode('product_project_test', 'product_project_test');
function product_project_test() {   
    $mydbTest = new wpdb($userdb,$mypass,$mydb,'localhost');
    $query = "SELECT * FROM wp_postmeta WHERE post_id=608 AND meta_key IN('_sale_price', '_regular_price')";

    $results = $mydbTest->get_results($query, ARRAY_A);
        foreach($results as $result){
        if($result['meta_key'] == '_sale_price'){
            $price= $result['meta_value'];
            $price= number_format($price, 2, '.', ',');
        } else if($result['meta_key'] == '_sale_price'){
            $old_price= $result['meta_value'];
            $old_price= number_format($old_price, 2, '.', ',');
        }
    };

    return '$ '.$price;

}

Share Improve this question edited Jan 16, 2020 at 17:39 Cristofer asked Jan 15, 2020 at 20:07 CristoferCristofer 11 bronze badge 2
  • Have you run the SQL in phpmyadmin to check you get a result? – TomC Commented Jan 15, 2020 at 21:06
  • yes, i copy and paste – Cristofer Commented Jan 15, 2020 at 23:17
Add a comment  | 

1 Answer 1

Reset to default 0

Your code looks correct to me, so I suspect there is some kind of issue connecting to the second database. Try the following which will log the last error:

$wpdb->show_errors(); 
if($wpdb->last_error !== '') :
    $wpdb->print_error()
endif;

If you are using multisite, then you will also need to add:

define( 'DIEONDBERROR', true );

Have a look here for more info: https://codex.wordpress/Class_Reference/wpdb#Show_and_Hide_SQL_Errors

本文标签: databaseShortcode select another dbbase