global $wpdb 显示垃圾数据以及我的选择查询的结果 [英] global $wpdb displaying garbage data along with results of my select query
问题描述
我在 MySQL 数据库中有一个自定义表,我正在尝试使用 global $wpdb
进行查询.我使用以下两个来源提供的信息定义了我的查询:
该查询似乎工作正常,因为它从我的自定义表中选择数据,但是除了表中可用的信息之外,输出似乎还包含垃圾/不必要的信息.
我希望它显示为表格/类似于它在 phpMyAdmin 中的显示方式,我可以在其中关联 SrNo、化合物strong> 等与表中的其他列:
add_shortcode('wpse_233031_shortcode', function(){全球 $wpdb;$myrows = $wpdb->get_results( "SELECT `SrNo`, `Compound` FROM PNaphtha");//$results = $wpdb->get_results( "SELECT `SrNo`, `Compound` FROM PNaphtha");ob_start();回声 var_dump($myrows );//返回 ob_get_clean();});
我得到以下结果
array(10) {[0]=>对象(标准类)#6275(2){[SrNo"]=>字符串(1)2"[化合物"]=>string(12) "松香酸"}[1]=>对象(标准类)#6274(2){[SrNo"]=>字符串(1)3"[化合物"]=>string(12) "苊"}[2]=>对象(标准类)#6273(2){[SrNo"]=>字符串(1)4"[化合物"]=>字符串(6)乙缩醛"}[3]=>对象(标准类)#6272(2){[SrNo"]=>字符串(1)5"[化合物"]=>string(12) "乙醛"}[4]=>对象(标准类)#6271(2){[SrNo"]=>字符串(1)6"[化合物"]=>字符串(9)乙酰胺"}[5]=>对象(标准类)#6270(2){[SrNo"]=>字符串(1)7"[化合物"]=>字符串(11)乙酰苯胺"}[6]=>对象(标准类)#6269(2){[SrNo"]=>字符串(1)8"[化合物"]=>字符串(11)醋酸"}[7]=>对象(标准类)#6268(2){[SrNo"]=>字符串(1)9"[化合物"]=>string(16) "醋酸酐"}[8]=>对象(标准类)#6267(2){[SrNo"]=>字符串(2)10"[化合物"]=>字符串(7)丙酮"}[9]=>对象(标准类)#6266(2){[SrNo"]=>字符串(2)11"[化合物"]=>string(19) "丙酮氰醇"}}
虽然我查询的所有信息都可以在这里找到,但也有很多不必要的信息.
我尝试了以下更改,但它们似乎都不起作用
$myrows = $wpdb->get_row( "SELECT `SrNo`, `Compound` FROM PNaphtha");//获取行output_type 更改为 ARRAY_A、ARRAY_N、OBJECT_Kecho var_dump 改为 echo 数组
如果您能就如何将结果格式化为表格或数组提出建议,我将不胜感激.此外,我已将上述代码放在我的主题的
functions.php
文件中.有没有更好的方法来做到这一点?解决方案因此,您将对象作为输出,这就是您在其中看到该信息的原因.进行以下更改.将ARRAY_A"(关联数组)添加到函数调用的末尾.
$wpdb->get_results( "SELECT `SrNo`, `Compound` FROM PNaphtha", ARRAY_A);
这将防止您的查询作为对象返回,并且您的
var_dump
将看起来像您期望的那样.I have a custom table in MySQL database, which I am trying to query using
global $wpdb
. I have defined my query using information available from the following two sources:- https://codex.wordpress.org/Class_Reference/wpdb
- https://wordpress.stackexchange.com/questions/233021/display-data-on-word-press-site-posts-and-pages-from-mysql-table
This is how the data is in phpMyAdmin:
The query seems to be working fine as it selects data from my custom table, however the output seems to contain garbage/unnecessary information apart from the information that is available in the table.
I want it to display as a table/similar to how it is displayed in phpMyAdmin, where I am able to associate the SrNo, Compound etc with other columns in the table:
add_shortcode('wpse_233031_shortcode', function(){ global $wpdb; $myrows = $wpdb->get_results( "SELECT `SrNo`, `Compound` FROM PNaphtha"); //$results = $wpdb->get_results( "SELECT `SrNo`, `Compound` FROM PNaphtha" ); ob_start(); echo var_dump($myrows ); //return ob_get_clean(); });
I get the following results
array(10) { [0]=> object(stdClass)#6275 (2) { ["SrNo"]=> string(1) "2" ["Compound"]=> string(12) "abietic acid" } [1]=> object(stdClass)#6274 (2) { ["SrNo"]=> string(1) "3" ["Compound"]=> string(12) "acenaphthene" } [2]=> object(stdClass)#6273 (2) { ["SrNo"]=> string(1) "4" ["Compound"]=> string(6) "acetal" } [3]=> object(stdClass)#6272 (2) { ["SrNo"]=> string(1) "5" ["Compound"]=> string(12) "acetaldehyde" } [4]=> object(stdClass)#6271 (2) { ["SrNo"]=> string(1) "6" ["Compound"]=> string(9) "acetamide" } [5]=> object(stdClass)#6270 (2) { ["SrNo"]=> string(1) "7" ["Compound"]=> string(11) "acetanilide" } [6]=> object(stdClass)#6269 (2) { ["SrNo"]=> string(1) "8" ["Compound"]=> string(11) "acetic acid" } [7]=> object(stdClass)#6268 (2) { ["SrNo"]=> string(1) "9" ["Compound"]=> string(16) "acetic anhydride" } [8]=> object(stdClass)#6267 (2) { ["SrNo"]=> string(2) "10" ["Compound"]=> string(7) "acetone" } [9]=> object(stdClass)#6266 (2) { ["SrNo"]=> string(2) "11" ["Compound"]=> string(19) "acetone cyanohydrin" } }
Although, all of the information I queried is available here, there is also a lot of unnecessary information.
I tried the following changes, however none of them seems to be working
$myrows = $wpdb->get_row( "SELECT `SrNo`, `Compound` FROM PNaphtha"); // get_row output_type changed to ARRAY_A, ARRAY_N, OBJECT_K echo var_dump changed to echo array
I will appreciate if you could please advise on how to get the results to format as a table or array. Also, I have placed the above code in the
functions.php
file of my theme. Is there a better way to do this?解决方案So, you're getting object as output, that's why you're seeing that information in there. Make the following change. Add 'ARRAY_A' (associative array) to the end of your function call.
$wpdb->get_results( "SELECT `SrNo`, `Compound` FROM PNaphtha", ARRAY_A);
That will keep your query from being returned as an object and your
var_dump
will look the way you expect.这篇关于global $wpdb 显示垃圾数据以及我的选择查询的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!