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
1 Answer
Reset to default 0Your 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
版权声明:本文标题:database - Shortcode select another dbbase 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744829587a2627270.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论