First i write manually update, delete, insert and select query and execute data with mysql_query
function
首先我用mysql_query函数手动编写更新,删除,插入和选择查询并执行数据
Like this:
Select query
$prefix = $wpdb->prefix;
$postSql = "SELECT DISTINCT post_id
FROM " . $prefix . "postmeta As meta
Inner Join " . $prefix . "posts As post
On post.ID = meta.post_id
Where post_type = 'product'
And post_status = 'publish'
And meta_key Like '%product_img%'";
$postQry = mysql_query($postSql);
while ($postRow = mysql_fetch_array($postQry)) {
$post_id = $postRow['post_id'];
}
Insert Query
$insert_images = "Insert Into " . $prefix . "postmeta(post_id,meta_key,meta_value) Value('$post_id','$meta_key','$data_serialize')";
mysql_query($insert_images);
Update Query:
$update_price = "Update " . $prefix . "postmeta
Set meta_key = 'wpc_product_price'
Where post_id = $supportMetaID
And meta_key Like '%product_price%'";
mysql_query($update_price);
Delete Query
mysql_query("Delete From " . $prefix . "postmeta Where meta_key IN ('product_img1','product_img2','product_img3')");
All queries are working perfectly ... but now i want to embed all queries in wordpress queries.
所有查询都运行良好...但现在我想在wordpress查询中嵌入所有查询。
I can also use wordpress queries like
我也可以使用wordpress查询
$wpdb->get_results( "SELECT id, name FROM mytable" );
$wpdb->insert(
'table',
array(
'column1' => 'value1',
'column2' => 123
),
);
$wpdb->update(
'table',
array(
'column1' => 'value1', // string
'column2' => 'value2' // integer (number)
),
array( 'ID' => 1 )
);
$wpdb->delete( 'table', array( 'ID' => 1 ) );
But you can see that i use and / or
conditions in my queries. So any body help me how can i embed my queries in wordpress
但是你可以在我的查询中看到我使用和/或条件。所以任何身体帮助我如何在wordpress中嵌入我的查询
2 个解决方案
#1
Use $wpdb->prepare
for the queries involving different criteria
使用$ wpdb-> prepare来处理涉及不同条件的查询
$wpdb->query(
$wpdb->prepare(
"Update " . $prefix . "postmeta
Set meta_key = %s
Where post_id = %d
And meta_key Like '%product_price%'" ,
'wpc_product_price',$supportMetaID
)
);
Also if your are inserting/deleting from postmeta is suggest you to use WP's builtin functions update_post_meta/delete_post_meta
此外,如果您正在插入/删除postmeta建议您使用WP的内置函数update_post_meta / delete_post_meta
#2
Well you can do it the wordpress way and all the security issues are already taken care by wordpress.
那么你可以用wordpress方式完成它,并且所有的安全问题都已经被wordpress处理了。
Adding/updating a post meta is as simple as
添加/更新post meta非常简单
update_post_meta( $post_id , 'key', 'value');
Fetching the post meta is as simple as
获取post meta非常简单
get_post_meta( $post_id , 'key', TRUE);
(Here true will return as a single variable, if you are expecing an array use FALSE.)
(这里true将作为单个变量返回,如果您要指定数组使用FALSE。)
Deleting a post meta is simple as
删除帖子元素很简单
delete_post_meta($post_id , 'key');
#1
Use $wpdb->prepare
for the queries involving different criteria
使用$ wpdb-> prepare来处理涉及不同条件的查询
$wpdb->query(
$wpdb->prepare(
"Update " . $prefix . "postmeta
Set meta_key = %s
Where post_id = %d
And meta_key Like '%product_price%'" ,
'wpc_product_price',$supportMetaID
)
);
Also if your are inserting/deleting from postmeta is suggest you to use WP's builtin functions update_post_meta/delete_post_meta
此外,如果您正在插入/删除postmeta建议您使用WP的内置函数update_post_meta / delete_post_meta
#2
Well you can do it the wordpress way and all the security issues are already taken care by wordpress.
那么你可以用wordpress方式完成它,并且所有的安全问题都已经被wordpress处理了。
Adding/updating a post meta is as simple as
添加/更新post meta非常简单
update_post_meta( $post_id , 'key', 'value');
Fetching the post meta is as simple as
获取post meta非常简单
get_post_meta( $post_id , 'key', TRUE);
(Here true will return as a single variable, if you are expecing an array use FALSE.)
(这里true将作为单个变量返回,如果您要指定数组使用FALSE。)
Deleting a post meta is simple as
删除帖子元素很简单
delete_post_meta($post_id , 'key');