我最近将旧网站从wordpress版本3.4升级到4.4。突然,我收到了一条PHP警告,如下所示:
警告:缺少wpdb :: prepare()的参数2,在第1543行的..... / wp-content / themes / ... functions / admin-functions.php中调用,并在....中定义...第1246行的public / wp-includes / wp-db.php
以下是 admin-functions.php 的代码:
global $wpdb;
$query = "SELECT *,count(*) AS used FROM $wpdb->postmeta WHERE meta_key = '_wp_page_template' AND meta_value = '$filename' GROUP BY meta_value";
$results = $wpdb->get_row($wpdb->prepare($query),'ARRAY_A'); // Select thrid coloumn accross
if(empty($results))
return false;
和 wp-db.php
public function prepare( $query, $args ) {
if ( is_null( $query ) )
return;
答案 0 :(得分:1)
您需要使用占位符并将变量添加为参数以进行准备,%s
是字符串值的占位符,假设它是引号中的字符串
$query = "SELECT *,count(*) AS used FROM $wpdb->postmeta
WHERE meta_key = '_wp_page_template' AND meta_value = %s
GROUP BY meta_value";
$results = $wpdb->get_row($wpdb->prepare($query,$filename),'ARRAY_A'); // Select thrid coloumn accross
答案 1 :(得分:0)
你需要传递$ meta_key作为第二个参数。
例如: -
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
versionCode 70
versionName "4.0.1"
multiDexEnabled = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
productFlavors {
def versionNumber = "4.0"
unitTest
up {
applicationId "com.livelys.up"
}
down {
applicationId "com.livelys.down"
}
strange {
applicationId "com.livelys.strange"
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/res']
}
strange {
java.srcDirs = ['src/strange/java']
resources.srcDirs = ['src/strange/res']
}
unitTest {
java.srcDirs = ['src/test/java']
assets {
srcDirs = ['src/test/assets']
}
}
}
}
def buildAliases = [
'default' : ['clean', 'assemble', 'testUnitTestDebug'],
'up' : ['clean', 'assembleUp', 'testUnitTestDebug'],
'down' : ['clean', 'assembleDown', 'testUnitTestDebug'],
'strange' : ['clean', 'doMagic', 'assembleStrange']
]
task magic << {
File srcFile = file('src/partner/res/values/magic.xml')
def content = srcFile.text
def pcode = System.getProperty('Param1')
def defaultLocation = System.getProperty('Param2')
srcFile.text = content.replaceAll("%%%P1%%%","${pcode}").replaceAll("%%%P2%%%","${defaultLocation}")
}
所以这里你的第二个参数是$ filename。
参考: - Class Reference / wpdb
答案 2 :(得分:0)
我建议您阅读$wpdb->prepare的文档。
准备需要传递至少两个参数。第一个是查询,使用占位符(%s
表示字符串,%d
表示数字),第二个是变量列表/值放入查询而不是占位符。
针对您的具体情况,我会以更长的格式进行演示,以便您清楚地看到:
global $wpdb;
// Old query. Let's get it ready for prepare...
// $query = "SELECT *,count(*) AS used FROM $wpdb->postmeta WHERE meta_key = '_wp_page_template' AND meta_value = '$filename' GROUP BY meta_value";
// New query, ready for prepare:
$query = "SELECT *,count(*) AS used FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s GROUP BY meta_value";
// Now we pass that into prepare, along with the two values we want replaced
$wpdb->prepare($query, '_wp_page_template', $filename);
// And we execute the query...
$results = $wpdb->get_row($query,'ARRAY_A');
请注意,您可以根据需要使用占位符作为多个值,但您可以将它们用于用户输入的任何值。例如,如果值可以通过表单发布,则您绝对要对该值使用prepare。例如:
$post_id = $_POST['post_id'];
$query = 'SELECT * FROM $wpdb->postmeta WHERE post_id = %d';
$query = $wpdb->prepare($query, $post_id);
希望这有帮助!