使用 PHP 中的 mysql 数组数据
work with mysql array data in PHP
在这种情况下如何转换数组数据以使用它?特别是在 wordpress 中。
当我打印结果时,我得到了 stdClass 对象。
Array
(
[0] => stdClass Object
(
[posts_id] => 336
[value] => 8
[count(*)] => 2
)
[1] => stdClass Object
(
[posts_id] => 329
[value] => 2
[count(*)] => 1
)
[2] => stdClass Object
(
[posts_id] => 327
[value] => 3
[count(*)] => 1
)
[3] => stdClass Object
(
[posts_id] => 338
[value] => 1
[count(*)] => 1
)
)
这是我的查询
<?php global $wpdb;
$test = $wpdb->get_results('select
posts_id,
value,
count(*)
From
wp_mrp_rating_item_entry_value
group by
posts_id,
value
order by
count(*) desc');
echo '<pre>';
print_r($test);
echo '</pre>';
如何从这个数组中取出数据并保存到另一个数据库中table?或者我怎样才能至少将单个值保存到每个 id 的变量中?我试过分配但我还不知道所以我希望有人比我更了解它?感谢您的帮助。
//Just use type cast brother. Use
$test = (array) $test;
//print_r($test);
foreach($test as $item) :
//if you want to place into the db
$wpdb->query( $wpdb->prepare(
"INSERT INTO $wpdb->name_of_wp_table_goes_here
( post_id,value,count )
VALUES (".$item['post_id'].", '".$item['value']."', ".$item['count'].")
%d",
1
) );
endforeach;
在Wordpress中使用get_results时可以选择脱水模式:
output_type
One of three pre-defined constants. Defaults to OBJECT.
OBJECT - result will be output as an object.
ARRAY_A - result will be output as an associative array.
ARRAY_N - result will be output as a numerically indexed array.
因此,如果您这样做:
$test = $wpdb->get_results('select
posts_id,
value,
count(*)
From
wp_mrp_rating_item_entry_value
group by
posts_id,
value
order by
count(*) desc', ARRAY_N);
你应该得到一个数组作为结果。
由于这是对象数组,您可以使用简单的 foreach 来访问您的值:
<?php
foreach ($test as $t) {
$t->post_id;
$t->value;
// You can insert your values in different table here
}
?>
对于计数,您可以将查询稍微更改为:
SELECT posts_id, value, COUNT(*) AS cnt FROM
wp_mrp_rating_item_entry_value
GROUP BY
posts_id,
value
ORDER BY
COUNT(*) DESC
因此您在 foreach 循环中的计数将可访问 $t->cnt。
foreach($test as $item) :
$ID = $item->posts_id;
var_dump($ID); //test variable
//if you want to place into the db
//DB CODE>>>
$firstName = "Dylan" //example of field in db to change
$LastName = "bloggs" //example of second field in db to change
$wpdb->query( $wpdb->prepare(
"
INSERT INTO $wpdb->name_of_wp_table_goes_here
( name_of_field_goes_here )
VALUES ( %d, %s, $s )
",
$ID,
$field
) );
//<<<DB END
endforeach;
如果要手动存储到数据库中,请删除数据库代码。
您也可以用同样的方式访问其他键。例如,$value = $item->value; -- 您可以将它放在 foreach 循环中并继续。
你有一个对象数组,这是一个非常好的结构。您需要做的就是处理数组。
我无法帮助您使用 WordPress 机制进行 table 插入,但伪代码应该是这样的。
首先给伯爵取一个更有用的名字是个好主意
$test = $wpdb->get_results('select
posts_id, value,count(*) as how_many
From wp_mrp_rating_item_entry_value
group by
posts_id, value
order by count(*) desc');
现在处理对象数组的每个事件
foreach ($test as $obj) {
// Use these fields in an Insert statement
$sql = "INSERT INTO TableName (f1,f2,f3)
VALUES ({$obj->post_id},
{$obj->value},
{$obj->how_many}");
// WP command to issue a query
}
在这种情况下如何转换数组数据以使用它?特别是在 wordpress 中。 当我打印结果时,我得到了 stdClass 对象。
Array
(
[0] => stdClass Object
(
[posts_id] => 336
[value] => 8
[count(*)] => 2
)
[1] => stdClass Object
(
[posts_id] => 329
[value] => 2
[count(*)] => 1
)
[2] => stdClass Object
(
[posts_id] => 327
[value] => 3
[count(*)] => 1
)
[3] => stdClass Object
(
[posts_id] => 338
[value] => 1
[count(*)] => 1
)
)
这是我的查询
<?php global $wpdb;
$test = $wpdb->get_results('select
posts_id,
value,
count(*)
From
wp_mrp_rating_item_entry_value
group by
posts_id,
value
order by
count(*) desc');
echo '<pre>';
print_r($test);
echo '</pre>';
如何从这个数组中取出数据并保存到另一个数据库中table?或者我怎样才能至少将单个值保存到每个 id 的变量中?我试过分配但我还不知道所以我希望有人比我更了解它?感谢您的帮助。
//Just use type cast brother. Use
$test = (array) $test;
//print_r($test);
foreach($test as $item) :
//if you want to place into the db
$wpdb->query( $wpdb->prepare(
"INSERT INTO $wpdb->name_of_wp_table_goes_here
( post_id,value,count )
VALUES (".$item['post_id'].", '".$item['value']."', ".$item['count'].")
%d",
1
) );
endforeach;
在Wordpress中使用get_results时可以选择脱水模式:
output_type One of three pre-defined constants. Defaults to OBJECT. OBJECT - result will be output as an object. ARRAY_A - result will be output as an associative array. ARRAY_N - result will be output as a numerically indexed array.
因此,如果您这样做:
$test = $wpdb->get_results('select
posts_id,
value,
count(*)
From
wp_mrp_rating_item_entry_value
group by
posts_id,
value
order by
count(*) desc', ARRAY_N);
你应该得到一个数组作为结果。
由于这是对象数组,您可以使用简单的 foreach 来访问您的值:
<?php
foreach ($test as $t) {
$t->post_id;
$t->value;
// You can insert your values in different table here
}
?>
对于计数,您可以将查询稍微更改为:
SELECT posts_id, value, COUNT(*) AS cnt FROM
wp_mrp_rating_item_entry_value
GROUP BY
posts_id,
value
ORDER BY
COUNT(*) DESC
因此您在 foreach 循环中的计数将可访问 $t->cnt。
foreach($test as $item) :
$ID = $item->posts_id;
var_dump($ID); //test variable
//if you want to place into the db
//DB CODE>>>
$firstName = "Dylan" //example of field in db to change
$LastName = "bloggs" //example of second field in db to change
$wpdb->query( $wpdb->prepare(
"
INSERT INTO $wpdb->name_of_wp_table_goes_here
( name_of_field_goes_here )
VALUES ( %d, %s, $s )
",
$ID,
$field
) );
//<<<DB END
endforeach;
如果要手动存储到数据库中,请删除数据库代码。
您也可以用同样的方式访问其他键。例如,$value = $item->value; -- 您可以将它放在 foreach 循环中并继续。
你有一个对象数组,这是一个非常好的结构。您需要做的就是处理数组。
我无法帮助您使用 WordPress 机制进行 table 插入,但伪代码应该是这样的。
首先给伯爵取一个更有用的名字是个好主意
$test = $wpdb->get_results('select
posts_id, value,count(*) as how_many
From wp_mrp_rating_item_entry_value
group by
posts_id, value
order by count(*) desc');
现在处理对象数组的每个事件
foreach ($test as $obj) {
// Use these fields in an Insert statement
$sql = "INSERT INTO TableName (f1,f2,f3)
VALUES ({$obj->post_id},
{$obj->value},
{$obj->how_many}");
// WP command to issue a query
}