网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

试图用PHP在Wordpress中获得mySQL的平均值,对象作为数组返回,需要它作为字符串

GG网络技术分享 2025-03-18 16:12 8


问题描述:

I have a custom table in my wordpress install, and I want to get an average out of a column on the table. I\'m using the following PHP:

 $latavg = $wpdb->get_results(\"SELECT AVG(stop_lat) FROM stops_txt\"); 

However, when I want to use the average later on as a string, it returns as \'ARRAY\', yet attempting to use PRINT_R to view the array reveals nothing. The Column in the table is a DECIMAL data-type, what am I missing?

网友观点:

When you just want a single variable instead of a whole row or column in wpdb, use get_var() instead:

$latavg = $wpdb->get_var(\\\"SELECT AVG(stop_lat) FROM stops_txt\\\"); 

Here is the complete wpdb reference.

标签:

提交需求或反馈

Demand feedback